Skip to content

Instantly share code, notes, and snippets.

@wendal
Created April 15, 2012 05:45
Show Gist options
  • Save wendal/2390303 to your computer and use it in GitHub Desktop.
Save wendal/2390303 to your computer and use it in GitHub Desktop.
Lua中拿到os.execute的输出结果
-- from http://stackoverflow.com/questions/132397/get-back-the-output-of-os-execute-in-lua
function os.capture(cmd, raw)
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
if raw then return s end
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
@hupantingxue
Copy link

How to get the output of os.execute(cmd)?
what is 'raw'?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment