如何在lua中获取shell脚本的返回码? [英] How to get the return code of a shell script in lua?

查看:388
本文介绍了如何在lua中获取shell脚本的返回码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在lua中执行脚本:

I'm executing a script in lua:

os.execute("sh manager/scripts/update_system.sh" .. " " .. f)

我想获取脚本的输出(如果退出状态为7,则返回7).

And I want to get the output of the script( if exit status is 7 returns 7).

我尝试过

local output = os.execute("sh manager/scripts/update_system.sh" .. " " .. f)
print(output)

但是它返回一些奇怪的数字,例如512

but it returns some weird numbers like 512

有什么办法解决这个问题吗?

Any idea how to resolve this?

推荐答案

这对Lua 5.1和Lua 5.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

这篇关于如何在lua中获取shell脚本的返回码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆