问题
I'm executing a script in lua:
os.execute("sh manager/scripts/update_system.sh" .. " " .. f)
And I want to get the output of the script( if exit status is 7 returns 7).
I tried
local output = os.execute("sh manager/scripts/update_system.sh" .. " " .. f)
print(output)
but it returns some weird numbers like 512
Any idea how to resolve this?
回答1:
It seems like the outputs of os.execute are all 256 multiples. Don't ask me why, it must be a bug.
So I did this:
local exit = os.execute("sh manager/scripts/update_system.sh" .. " " .. f)
local value = exit / 256
print(value)
It works but I wonder if there is another solution.
回答2:
This works for both Lua 5.1 and Lua 5.2
exit_code = io.popen'your_command \necho _$?':read'*a':match'.*%D(%d+)'+0
来源:https://stackoverflow.com/questions/23826755/how-to-get-the-return-code-of-a-shell-script-in-lua