Spawning Processes

Jul 1, 2023  │  m. Jul 2, 2023 by Gleb Buzin

This is how to spawn external processes in Lua

local exit_code = os.execute("uname -s")

print(exit_code)
Linux
0

If you want to capture the output

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

output = os.capture("uname -s")
print(output)
Linux


Next: LuaRocks (package manager)