lua-users home
lua-l archive

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



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