[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: obtaining process exit code from popen()
- From: steve donovan <steve.j.donovan@...>
- Date: Sat, 6 Jun 2009 13:17:42 +0200
On Sat, Jun 6, 2009 at 11:54 AM, steve
donovan<steve.j.donovan@gmail.com> > This is a fuller elaboration of
the hack; you get the status code any
> way (because you will need to strip it out with some pattern)
Just for the record, this version covers most of the cases; it
redirects stderr, so that you can use to capture the full output of
'gcc' say.
function command(cmd,no_lf)
local f = io.popen(cmd..' 2>&1; echo "-retcode:$?"' ,'r')
local l = f:read '*a'
f:close()
local i1,i2,ret = l:find('%-retcode:(%d+)\n$')
if no_lf and i1 > 1 then i1 = i1 - 1 end
l = l:sub(1,i1-1)
return l,tonumber(ret)
end
res,ret = command (arg[1],true)
print('result "'..res..'"')
print('retcode',ret)
Now the next question was whether it this can be done on Windows.
Not..quite. It has all the bits, you can do the '2>&1' trick (I didn't
know that!), the command separator is '&' not ';', and '%ERRORLEVEL%
does give the return code of the last command. Unfortunately, its
value is not updated immediately in compound commands! I will leave
this to the last surviving Windows power users ;)
It is a hack, true, but that's why it needs to be hidden in a library
steve d.