Home > Enterprise >  How to run an executable and get its return value in LUA?
How to run an executable and get its return value in LUA?

Time:10-25

I'm trying to run an executable using os.execute(), however, I need to know its exit value, whether it's 0 or something else. Any advice?

CodePudding user response:

In Lua 5.2 , os.execute returns three values: success, reason, code. You want code when reason is "exit".

CodePudding user response:

You can use one of the three returns of os.execute() to conditionally decide what to do.
This example demonstrate it in a do end block in an interactive Lua console session...

$ /usr/local/bin/lua
Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
> do local bool, stat, rc = os.execute('false') if bool then return rc else return rc end end
1 -- From: else return rc
> do local bool, stat, rc = os.execute('true') if bool then return rc else return rc end end
0 -- From: then return rc
  • Related