How to get the return code of a shell script in lua?

限于喜欢 提交于 2019-12-10 18:13:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!