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?
Asked
Active
Viewed 242 times
0
-
Since you specifically only asked for the exit value I'm not sure if its an duplicate, but check out https://stackoverflow.com/questions/132397/get-back-the-output-of-os-execute-in-lua – Luke100000 Oct 21 '21 at 09:27
2 Answers
3
In Lua 5.2+, os.execute returns three values: success, reason, code. You want code when reason is "exit"
.

lhf
- 70,581
- 9
- 108
- 149
0
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

koyaanisqatsi
- 2,585
- 2
- 8
- 15