68

Is it possible to read the following from the local variable in Lua?

local t = os.execute("echo 'test'")
print(t)

I just want to achieve this: whenever os.execute returns any value, I would like to use it in Lua - for example echo 'test' will output test in the bash command line - is that possible to get the returned value (test in this case) to the Lua local variable?

0

5 Answers 5

124

You can use io.popen() instead. This returns a file handle you can use to read the output of the command. Something like the following may work:

local handle = io.popen(command)
local result = handle:read("*a")
handle:close()

Note that this will include the trailing newline (if any) that the command emits.

Sign up to request clarification or add additional context in comments.

6 Comments

Getting this message: 'popen' not supported.
@Cyclone: According to the manual, "This function is system dependent and is not available on all platforms". What platform are you trying this on? The only workaround I can think of given the available functions is to use os.execute() but redirect standard output to a known temporary file, and then read the temporary file afterwards.
I'm using FreeBSD 8.2. About your second suggestion - could you give me some example of how it should look like? And how about the performace? Its a lot of writing/reading.
@Cyclone: Something like os.execute(command .. " >/tmp/foo") (where /tmp/foo is replaced by an actual somewhat unique path, however you want to calculate it).
Could I do the opposite of what the question asks? Return a Lua value in the os shell.
|
5
function GetFiles(mask)
   local files = {}
   local tmpfile = '/tmp/stmp.txt'
   os.execute('ls -1 '..mask..' > '..tmpfile)
   local f = io.open(tmpfile)
   if not f then return files end  
   local k = 1
   for line in f:lines() do
      files[k] = line
      k = k + 1
   end
   f:close()
   return files
 end

Comments

3

If supported on your system, io.popen is better suited for this use-case than os.execute. The later only returns the exit status not the output.

-- runs command on a sub-process.
local handle = io.popen('cmd')
-- reads command output.
local output = handle:read('*a')
-- replaces any newline with a space
local format = output:gsub('[\n\r]', ' ')

Working example:

local handle = io.popen('date +"%T.%6N"')
local output = handle:read('*a')
local time = output:gsub('[\n\r]', ' ')
handle:close()
print(time .. 'DEBUG: Time recorded when this event happened.')

Comments

-6

Lua's os.capture returns all standard output, thus it will be returned into that variable.

Example:

local result = os.capture("echo hallo")
print(result)

Printing:

hallo

2 Comments

I don't think this method is part of the os module. lua.org/manual/5.3/manual.html#6.9
This requires the snippe from this answer and it uses popen.
-24

Sorry, but this is impossible. If the echo programm exit with success it will return 0. This returncode is what the os.execute() function get and returned too.

if  0 == os.execute("echo 'test'") then 
    local t = "test"
end

This is a way to get what you want, i hope it helps you.

Another Tip for getting the return code of a function is the Lua reference. Lua-Reference/Tutorial

1 Comment

Note to reviewers: Wrong answers are not Very Low Quality. The correct review for this is "Looks OK."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.