Capturing output from operating system commands

Posted by Nick Gammon on Tue 07 Dec 2004 09:09 PM — 6 posts, 24,761 views.

Australia Forum Administrator #0
I had this code in another thread, but it was a bit hard to find, so I'll put it here in the Lua section.

Here is an example of executing an operating system command, and capturing the output.


-- get a temporary file name
n = os.tmpname ()

-- execute a command
os.execute ("dir > " .. n)

-- display output
for line in io.lines (n) do
  print (line)
end

-- remove temporary file
os.remove (n)



Greece #1
I found an ActiveX control to do that, tell me if anyone wants it (can't remember the name at the moment). Does the program have to stop for this lua code to work, or can it do it as it's running?
Australia Forum Administrator #2
Still trying to get the chess game to work, eh?

In this case, it runs the command and exits. Conceivably you might do it with piping, but that isn't enabled right now. Plus, I doubt it a bit.
Greece #3
I tried piping, it doesn't work. The chess system runs great with that ActiveX control (if only Crafty had coordinate notation, aaargh).
Australia Forum Administrator #4
Here's a suggestion - write your own chess game in Lua. ;)
Greece #5
I'm not trying to make a game, I'm trying to get the engine (the AI) to work, which I did with that control. I still haven't learned Lua :P