lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2018-07-16 21:38 GMT+02:00 Egor Skriptunoff <egor.skriptunoff@gmail.com>:
> On Sat, Jan 24, 2015 at 2:56 AM, William Ahern wrote:
>>
>> On Fri, Jan 23, 2015 at 02:21:58PM -0800, William Ahern wrote:
>> > My recently released runlua utility has an option to load and skip over
>> > any
>> > initial shell code in the file. But I just discovered a neat trick which
>> > obviates the need for such a feature.
>> >
>> >   #!/bin/sh
>> >   _=[[
>> >   exec lua "$0" "$@"
>> >   ]]
>> >   print("Running", _VERSION)
>> >   print("Options", ...)
>> >
>> > Probably others have figured this out, too, but it's new to me.
>>
>> Rena posted nearly the same thing a few years ago:
>>
>> http://lua-users.org/lists/lua-l/2012-05/msg00171.html
>>
>> His example is also portable. It doesn't rely on anything unique to bash.
>>
>
>
> Here is universal (Windows + Linux) shebang for Lua scripts.
> It is compatible with all Lua versions: Lua 5.0/5.1/5.2/5.3/5.4/LiaJIT.
> An executable or symlink with name "lua" must exist in the $PATH (or %PATH%)
> on your system.
>
> #!/bin/sh
> rem=rem--[[
> : '--------------- This is batch script ---------------
> @cls&  setlocal&  set this_file=%~0&  if exist "%~0.bat" set
> this_file=%~0.bat
> @lua "%this_file%" %*&  goto :eof
> '#---------------- This is shell script ---------------
> exec lua "$0" "$@"
> ]]---------------- This is Lua script -----------------
> print("Hi")
> print("Press Enter to exit")
> io.read()
>
> Useful properties:
> 1) All arguments to batch/shell script are passed as arguments to Lua
> script;
> 2) Exit code of Lua script is propagated to exit code of this batch/shell
> script;
> 3) When invoked from command line in existing console, the control returns
> to the same console after Lua script terminated;
> 4) When invoked by double-clicking on .bat/.sh file, the console is closed
> on Lua script termination;
> 5) When run under Windows, some unwanted text is printed due to first two
> lines of the script, but the command "clr" clears the screen (unfortunately,
> all previous text written to this console is also lost);
> 6) When run under Linux, a garbage is assigned to env variable "rem" (do
> lowercase env variables never used in Linux?);
>
> To make the same script working simultaneously in both Windows and Linux,
> set the file extension to ".bat" and all newlines to LF.

() () () () () !