开发者

Getting return status AND program output

开发者 https://www.devze.com 2023-04-09 19:28 出处:网络
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 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:

  • os.execute() returns the status code
  • io.popen() returns a file handler that can be used to read process output

However I need both. Writing a wrapper function that runs both functions behind the scene is no开发者_开发技巧t 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.


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!


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


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.


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.

  • http://lua-users.org/lists/lua-l/2004-05/msg00005.html
  • http://lua-users.org/lists/lua-l/2011-02/msg00387.html


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消