When I do an "os.execute" in Lua, a console quickly pops up, executes the command, then closes down. But is there some way of getting back the console output only using the standard Lua libraries?
The Lua os. execute() function can be used to run an external program from SIMION. The Lua io. popen() function is similar but captures any data written to standard output as a string (or writes a string to standard input).
Every shell command can be invoked as a Lua function. For example, calling echo hello world in Lua would be echo('Hello', 'world') . To achieve this I added a handler function for the missing table items in the globals table.
If you have io.popen, then this is what I use:
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
If you don't have io.popen, then presumably popen(3) is not available on your system, and you're in deep yoghurt. But all unix/mac/windows Lua ports will have io.popen.
(The gsub
business strips off leading and trailing spaces and turns newlines into spaces, which is roughly what the shell does with its $(...)
syntax.)
I think you want this http://pgl.yoyo.org/luai/i/io.popen io.popen. But it's not always compiled in.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With