Obtaining a return status and exiting the program

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

I searched the Internet and could not find something that does what I need. However, I found out that in Lua:

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

However, I need both. Writing a wrapper function that performs both functions behind the scenes is not an option due to the costs of the process and, possibly, the resultant changes in successive runs. I need to write a function like this:

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

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

PS. Linux target launch system.

+6
source share
5 answers

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()} 

Hope this helps ...

+8
source

This function is provided in C by pclose .

Upon successful return, pclose () returns the completion status of the shell interpreter.

The interpreter returns the completion status of its child.

But Lua is not doing it right ( io.close always returns true). I did not break into these topics, but some people complain about this brain damage.

+2
source

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

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

  • executes the correct executable file by writing the exit code to the shell variable,
  • prints a newline and terminal character / line to standard
  • displays the value of shell variables (exit code) to standard

Then write down the entire output of io.popen and parse it back.

Full disclosure: I'm not a Lua developer.

+1
source

I cannot 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 
+1
source

yes, you are correct that os.execute() returns, and it is very simple, if you understand how to execute your command using and with lua you can also find out how many variables it returns, and this may take some time, but i think you can try

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

for my example, a is the first argument returned, b is the second argument returned, etc. I think I answered your question correctly based on what you ask.

0
source

Source: https://habr.com/ru/post/898350/


All Articles