lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Luciano!

Try the following:

function execute(command)
    local lines = {}
    local file = io.popen(command)
    for line in file:lines() do
        table.insert(lines, line)
    end
    file:close()
    return lines
end

This does not explain why your code does not what you want, but is should work.

You can list the whole table with:

output = execute('ls')
for n,line in output do print(n,line) end

Maybe your ls does not output two lines..

Regards,

    Jorge-León


On 11/06/11 15:18, Luciano de Souza wrote:
Hello listers,

I am trying to load a table with lines from a shell output. However, I am not able to understand what is wrong.

#!./usr/bin/env lua

function execute(command)
local lines = {}
local file = io.popen(command)
 while true do
line = file:read('*l')
table.insert(lines, line)
if line == nil then break
end
file:close()
return lines
end

output = execute('ls')
print(output[2])

Each line of the table should contain a line from the output, but in stead of it, I have a looping.

Luciano