How to properly set up environment for Lua - lua

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.

Related

How can I use docker env files in shell scripts?

Docker env files look similar to shell scripts defining variables.
Unfortunately in env files, values cannot be quoted and so simply sourcing them only works if there are no "special" characters in the values.
VAR1=This_works
VAR2=This will not work
VAR3=This won't either
Is there any way you can use these files in a shell script?
My current approach is this:
eval $( perl -ne '
s/\x27/\x27\\\x27\x27/g;
s/^(\w+)=(.+)$/export $1=\x27$2\x27/ and print
' "path/to/env_file" )
So I'm searching for any quote in each line of the env file and replace it by '\''.
Then I'm modifying each line which starts with an identifier (\w+) followed by a = and any text (.+). So the VAR3 will become: export VAR3='This won'\''t either.
The modified line is then printed.
Everything which was printed is eval-ed and so the variables will be available in my shell's environment.
Are there other proposals how to achieve this?

Run script on start lua interpreter

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.

How to call multiple bash command and ssh in Ruby?

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.

What command should I be using to launch a program from the Lua Intepreter?

I have been attempting to troubleshoot how to launch a program I write in Lua, and it seems to me that I should be launching the program from the Lua Interpreter. The First Edition of Programming in Lua tells me I should use the command prompt> lua hello.lua. The name of my program is "hello.lua" and it is in the same folder as the Interpreter but I get the error message '=' expected near '>'. What command should I be using? Or am i doing something wrong?
Under Windows (administrator console):
ftype Lua.File=C:\utils\lua.exe "%1" %*
(where c:\utils\lua.exe is the actual path of your Lua interpreter)
assoc .lua=Lua.File
Now, you can type: hello.lua directly at the command prompt
And, if in "Computer/Properties/Advanced/Environment Variables" (Win7 example) you add .LUA to the PATHEXT variable, you can simply type: hello at the command prompt, without the extension.
(I guess you are on some Unix or POSIX system)
Just type lua hello.lua; the prompt> string is given by your shell (and the prompt is generally something different and configurable).
By typing literally prompt> you are asking your shell to run a program -or a command- named prompt and to redirect its stdout (to what follows the >)

Ruby on Rails script console

I wasn't able to run ./script/console previously, and it used to throw an error since my script console file had included #!/usr/bin/env ruby19. After doing hit and trial I fixed this error by replacing #!/usr/bin/env ruby19 with #!/usr/bin/env ruby.
What does the above line do?
Versions:
Ruby: 1.9.2-p180
Ruby on Rails: 2.3.5
The #! (hash bang) in the first line of a text file tells the program loader in most *nix systems to invoke the program that is specified next (in this case, /usr/bin/env) with any params supplied (in this case, ruby).
/usr/bin/env is just a portable way of looking in your environment for program named in the first argument. Here it is the Ruby interpreter. If the Ruby interpreter is in your PATH, env will find it and run it using the rest of the file as input.
You probably didn't have a program named ruby19 in your PATH, so you'd get the error. You do have a program named ruby, so that works.
The shebang line in a Unix script is supposed to specify a full path, so this:
#!/usr/local/bin/ruby
is valid but this is not:
#!ruby
The problem with the first form is that, well, you need to use a full path but the actual path won't be the same on all systems. The env utility is often used to allow a script to search the PATH environment variable for the appropriate interpreter, env should always be in /usr/bin/env so you can safely use that as a full path and then let env search the PATH for the named interpreter.
From the fine manual:
env [-i] [name=value]... [utility [argument...]]
The env utility shall obtain the current environment, modify it according to its arguments, then invoke the utility named by the utility operand with the modified environment.
That isn't terribly helpful in your case but I figured I should include it anyway. The shebang use of env is a bit of a hack that doesn't use the intended behavior of env.

Resources