lua-users home
lua-l archive

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


2013/12/28 Philipp Janda <siffiejoe@gmx.net>:

> But you can find out (in Lua 5.2) by calling the `close` method on the
> returned file pointer:
>
>     > print( io.popen( "no_such_program" ):close() )
>     sh: 1: no_such_program: not found
>     nil     exit    127
>     > print( io.popen( "false" ):close() )
>     nil     exit    1
>     > print( io.popen( "true" ):close() )
>     true    exit    0

So one could use this instead of io.popen?

function popen(prog,mode)
local cond,msg,code = io.popen(prog,mode):close()
if code==127 then error("'"..prog.."' not found") end
return io.popen(prog,mode)
end