[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: os.execute on steroids:
- From: Asko Kauppi <asko.kauppi@...>
- Date: Mon, 8 Mar 2004 10:12:06 +0200
Hi, as part of my Lua build engine, I've made some helper functions
that I'm now documenting.
One of them is here, an expansion of 'os.execute()' that catches the
stdout output and returns it (without anything being written on screen)
to the Lua function. I think this is very practical, and should
probably be part of Lua standard libraries? What do you think?
-ak
-----
-- [stdout_str]= ShellCommand( cmd_str )
--
function ShellCommand( cmd )
--
local tmpname= Loc_TmpName()
local str= nil
local rc= os.execute( cmd.." >"..tmpname )
if rc==0 then
local f= io.open( tmpname, OPEN_RT )
ASSUME(f)
str= f:read("*a") -- read all at once
f:close()
end
os.remove(tmpname)
-- Remove terminating linefeed, if any (eases up one-line analysis)
--
if str then
if string.sub( str, -1 ) == '\n' then
str= string.sub( str, 1, -2 )
end
end
return str
end