sadashiv30
sadashiv30

Reputation: 711

How to execute lua bytecode genrated by luac on linux

I have a simple lua source code called hello.lua

print('Hello Lua')

I complied this file to bytecode on a RedHat Linux machine , using Lua5.3.4 as follows:

luac -o hello.luac  hello.lua
chmod +x hello.luac
./hello.luac 
bash: ./hello.luac: cannot execute binary file

The architecture should be fine I guess. I cant figure what is wrong.

Upvotes: 4

Views: 3202

Answers (3)

D.Liu
D.Liu

Reputation: 146

The shebang is removed by luac even if the lua source file has it. In some case when it's necessary to run the compiled binary without any parameter, e.g. CGI, you can add the shebang manually at the top of the luac file:

luac -o hello.luac hello.lua
echo '#!/usr/bin/env lua' > shebang
cat shebang hello.luac > hello.luac2
mv hello.luac2 hello.luac
chmod +x hello.luac
./hello.luac

Upvotes: 2

Adam
Adam

Reputation: 3103

As @lhf states in his answer Lua byte code is executed using the Lua interpreter, and as the manual suggests:

To allow the use of Lua as a script interpreter in Unix systems, the standalone interpreter skips the first line of a chunk if it starts with #. Therefore, Lua scripts can be made into executable programs by using chmod +x and the #! form.

Add a shebang as the first line of your script:

#!/usr/bin/env lua
print('Hello Lua')

Upvotes: 0

lhf
lhf

Reputation: 72312

Precompiled Lua programs are run exactly the same way as source:

lua hello.luac

Upvotes: 4

Related Questions