Skip to content

Instantly share code, notes, and snippets.

@pocco81
Forked from dukeofgaming/os.capture
Created September 15, 2021 03:12
Show Gist options
  • Save pocco81/e08783301334dbc61929448049f47873 to your computer and use it in GitHub Desktop.
Save pocco81/e08783301334dbc61929448049f47873 to your computer and use it in GitHub Desktop.
Capture console output from Lua system call
---
-- Function to retrieve console output
--
function os.capture(cmd, raw)
local handle = assert(io.popen(cmd, 'r'))
local output = assert(handle:read('*a'))
handle:close()
if raw then
return output
end
output = string.gsub(
string.gsub(
string.gsub(output, '^%s+', ''),
'%s+$',
''
),
'[\n\r]+',
' '
)
return output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment