AlexStack
AlexStack

Reputation: 17411

Getting return status AND program output

I need to use Lua to run a binary program that may write something in its stdout and also returns a status code (also known as "exit status").

I searched the web and couldn't find something that does what I need. However I found out that in Lua:

However I need both. Writing a wrapper function that runs both functions behind the scene is not an option because of process overhead and possibly changes in result on consecutive runs. I need to write a function like this:

function run(binpath)
    ...
    return output,exitcode
end

Does anyone has an idea how this problem can be solved?

PS. the target system rung Linux.

Upvotes: 23

Views: 13240

Answers (6)

Jian
Jian

Reputation: 3258

This is how I do it.

local process = io.popen('command; echo $?') -- echo return code of last run command
local lastline
for line in process:lines() do
    lastline = line
end
print(lastline) -- the return code is the last line of output

If the last line has fixed length you can read it directly using file:seek("end", -offset), offset should be the length of the last line in bytes.

Upvotes: 5

aignas
aignas

Reputation: 1074

With Lua 5.2 I can do the following and it works

-- This will open the file
local file = io.popen('dmesg')
-- This will read all of the output, as always
local output = file:read('*all')
-- This will get a table with some return stuff
-- rc[1] will be true, false or nil
-- rc[3] will be the signal
local rc = {file:close()}

I hope this helps!

Upvotes: 24

joshua chris
joshua chris

Reputation: 53

yes , your are right that os.execute() has returns and it's very simple if you understand how to run your command with and with out lua you also may want to know how many variables it returns , and it might take a while , but i think you can try

local a, b, c, d, e=os.execute(-what ever your command is-)

for my example a is an first returned argument , b is the second returned argument , and etc.. i think i answered your question right, based off of what you are asking.

Upvotes: -3

David Qin
David Qin

Reputation: 365

I can't use Lua 5.2, I use this helper function.

function execute_command(command)
    local tmpfile = '/tmp/lua_execute_tmp_file'
    local exit = os.execute(command .. ' > ' .. tmpfile .. ' 2> ' .. tmpfile .. '.err')

    local stdout_file = io.open(tmpfile)
    local stdout = stdout_file:read("*all")

    local stderr_file = io.open(tmpfile .. '.err')
    local stderr = stderr_file:read("*all")

    stdout_file:close()
    stderr_file:close()

    return exit, stdout, stderr
end

Upvotes: 5

cnicutar
cnicutar

Reputation: 182664

This functionality is provided in C by pclose.

Upon successful return, pclose() shall return the termination status of the command language interpreter.

The interpreter returns the termination status of its child.

But Lua doesn't do this right (io.close always returns true). I haven't dug into these threads but some people are complaining about this brain damage.

Upvotes: 3

Ryan Schipper
Ryan Schipper

Reputation: 1029

If you're running this code on Win32 or in a POSIX environment, you could try this Lua extension: http://code.google.com/p/lua-ex-api/

Alternatively, you could write a small shell script (assuming bash or similar is available) that:

  • executes the correct executable, capturing the exit code into a shell variable,
  • prints a newline and terminal character/string onto standard out
  • prints the shell variables value (the exit code) onto standard out

Then, capture all the output of io.popen and parse backward.

Full disclosure: I'm not a Lua developer.

Upvotes: 1

Related Questions