lua-users home
lua-l archive

[Lua5.1] popen and exit code

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello,
I'd like to get exit code from shell commands executed with «popen».
According to this, it's possible:
http://www.luafaq.org/#T8.2.8
The «close» should return the exit code.
Fine, but there's two problems
#1 : the code returned is a boolean (i thought it would be an integer)
#2 : this boolean is always set to «true», whatever success of command shell is.
Here is my try:
#!/usr/bin/env lua
function shell (cmd)
 local f = io.popen (cmd, 'r')
 local buffer = f.read (f,'*a')
 local exit_code = f.close(f)
 return buffer, exit_code
end
buffer, exit_code = shell ("imsurethiscommandisinvalid")
if (exit_code==true) then
 io.write ("ERROR\n")
else
 print (buffer)
end
Thank you.

AltStyle によって変換されたページ (->オリジナル) /