How i can automatically run some script on lua without bat/shortcut, when i start interpreter? M.b. lua have some reserved file names like autorun or autoexec?
Just skip following text. Its for validator.
For portable use you can create bat file. Its dont like to me, but it works:
luajit.exe %~dp0%~n0.lua
that .bat run script with same name.
my_name.bat - run my_name.lua
123.bat - run 123.lua
Its not perfect but idk another decision.
Related
I'm trying to write (what I thought would be) a simple bash script that will:
run virtualenv to create a new environment at $1
activate the virtual environment
do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate file that activates the virtual environment. From the CLI, you run it using source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate", I get this:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate contains the correct path to the activate script, in fact I even test that a file is there before I call source. But source itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate, and calling source "$activate" in basically the same way I am.
Here is the script in its entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
If you're writing a bash script, call it by name:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
In the POSIX standard, which /bin/sh is supposed to respect, the command is . (a single dot), not source. The source command is a csh-ism that has been pulled into bash.
Try
. $env_name/bin/activate
Or if you must have non-POSIX bash-isms in your code, use #!/bin/bash.
In Ubuntu if you execute the script with sh scriptname.sh you get this problem.
Try executing the script with ./scriptname.sh instead.
best to add the full path of the file you intend to source.
eg
source ./.env instead of source .env
or source /var/www/html/site1/.env
could you please help me set up environment for Lua in Windows. I have added following C:\lua\bin to the PATH system env. variables. But when I try to run file for example test.lua with print("hello world") interpreter probably sees the file but I get this message in git-bash:
$ test.lua
/c/lua/bin/test.lua: line 1: syntax error near unexpected token "hello world"
/c/lua/bin/test.lua: line 1: print("hello world")
And when I try to open test.lua in cmd there are no errors just the Notepad with code opens...but code in file isnt executed.
Thank you
You can associate path extension with Lua executable like
ftype LuaScriptx86=c:\lua\x86\5.1\lua5.1.exe "%1" %*
assoc .lua=LuaScriptx86
(do not forget quotes around first argument)
And then add .LUA to PATHEXT env variable.
So you will be able run just test instead of lua test.lua
bash and cmd don't know that you want to execute the file using the Lua interpreter.
I assume that the Lua interpreter is at C:\lua\bin\lua.exe? If so, you should be able to run your script using the command lua test.lua. In bash, you might need to do /c/lua/bin/lua test.lua instead.
Example:
I have lua53.exe in the folder C:\Lua\
I did not add this path to system variable PATH.
This is my Lua file "a.lua" containing shebang:
$ cat a.lua
#!/c/Lua/lua53
print"Hello"
And this is how I run it:
$ ./a.lua
Hello
P.S.
You are allowed to terminate shebang line with Windows-style newline CR LF (such shebang will not work on *nix), so using Notepad to edit Lua files is Ok.
I want to do the following commands in ruby.
ssh into another computer using ssh example#example
set source file source ~/.profile
cd to/some/folder
call my shell script with parameters, a json formatted string ,./my_script.sh my_hash.to_json
However I am facing these problems:
I call them in one line using backticks, it works, but it is a very bad practice in my opinion because it is not readable nor it is maintainable.
On the other hand, when I call my_hash.to_json, the resulted string has non-escaped double quotes, How do I escape them?
I would recommend to view this tutorial for ssh with ruby. then make a shell script and move it to server and then execute like a single command.
create a single shell script file for example script1 and then execute it at once instead of executing each command individually.
open file script1 using any editor.
copy all commands to script1 (each command in new line).
script1 file should look like this
#!/bin/bash
ssh example#example
source ~/.profile
cd to/some/folder
save file
make this file executable using chmod +x script
execute it in ruby like this [backtick]./script1[backtick]
note: copy script1 to usr/bin to avoid "./" and then try command only script1.
Reference for passing arguments in shell script is here.
Let's say I have a Dart script called dart-test. I would like to distribute this script and make it so that users just have to put it in a folder in their $PATH, and execute it from anywhere just by typing dart-test in their terminal.
For the sake of this question, let's pretend I am the user test on my machine. I am on Mac OS X and installed the Dart binary with Homebrew. The dart binary lies in /home/test/.brew/bin and is in the $PATH.
Consequently, the following works:
$ cat <<HEREDOC > ~/.brew/bin/dart-test
#!/home/test/.brew/bin/dart
main() => print('Dart shebang works!');
HEREDOC
$ chmod u+x ~/.brew/bin/dart-test
$ dart-test
Dart shebang works!
The problem is that the Dart shebang I use is not portable, my script won't work on any other computer than mine. Is there a portable way to do this?
(Considering Dart is kind of like Python and Ruby in the way it executes, I just looked at the standard way of doing it in those two languages. The env binary.)
#!/usr/bin/env dart
Seems to be the way. It will look for dart binaries in the user's environment, and apparently enables simple Dart scripts to be executed from anywhere, provided the Dart VM is installed and in the $PATH.
This would be part of a reverse-engineering project.
To determine and document what a shell script (ksh, bash, sh) does, it is comfortable, if you have information about what other programs/scripts it calls.
How could one automate this task? Do you know any program or framework that can parse a shell script? This way for instance, I could recognize external command calls -- a step to the right direction.
For bash/sh/ksh, I think you can easily modify their source to log what has been executed. That would be a solution.
How about:
Get a list of distinct words in that script
Search $PATH to find a hit for each
?
bash -v script.sh ?
Bash's xtrace is your friend.
You can invoke it with:
set -x at the top of your script,
by calling your script with bash -x (or even bash --debugger -x),
or recursively by doing (set -x; export SHELLOPTS; your-script; )
If you can't actually run the script, try loading it into a text editor that supports syntax highlighting for Bash. It will color-code all of the text and should help indicate what is a reserved word, variable, external command, etc.