I have been trying to extract the result of an io.popen call. I can read the result of a command but can’t get any error information. Also when I check the type of the returned handle I get ‘userdata’. In this code the fHandle is ‘userdata’ and errMsg is ‘nil’
Any ideas on how the userdata result should be handled?
[code]-- get the directory list
local fHandle, errMsg = io.popen(‘ls’)
local fHandleType = type(fHandle)
luup.log('DEBUG: fHandle TYPE IS: '…fHandleType, 50)
local errMsgType = type(errMsg)
luup.log('DEBUG: errMsg TYPE IS: '…errMsgType, 50)[/code]
Turns out that file handle is never nil. It always returns a userdata structure, with this metatable stuff in it:
setvbuf=function: 0xdb6b20
lines=function: 0xcd3fb8
write=function: 0xc25e38
close=function: 0xcc6280
flush=function: 0xcd3f78
__gc=function: 0xcf5e48
read=function: 0xdb6ac0
seek=function: 0xcc6258
__index=table: 0xcc8c88
__tostring=function: 0xcf5e68
It appears the only way an error can be detected is by piping the stderr stream into the command result and and search for error messages. eg
//-----------------------------------------------------
local f = io.popen(‘ls 2>&1’)
local test = f:read()
luup.log (test)
return true
test shows the first file name in the directory
//-----------------------------------------------------
local f = io.popen(‘ls ABCXYZ 2>&1’)
local test = f:read()
luup.log (test)
return true
test shows ‘ls: ABCXYZ: No such file or directory’
//-----------------------------------------------------