lua-users home
lua-l archive

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


I'd like to get exit code from shell commands executed with «popen».

According to this, it's possible:

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,'*a')
   local exit_code = f.close(f)
   return buffer, exit_code

buffer, exit_code = shell ("imsurethiscommandisinvalid")
if (exit_code==true) then
   io.write ("ERROR\n")
   print (buffer)

Thank you.