I'm trying to create a program that runs a shell on the background and sends user commands to it for execution and return the result. This is the code:
--note: this runs on windows but I assume replacing "cmd" with "sh" it can run on linux as well
exe,err=io.popen("cmd > stdout.txt 2> stderr.txt");
if not exe then
print("Could not run command. Error: "..err)
return
else
print("Command run successfully... ready!")
end
stdout,err=io.open("stdout.txt","r")
if not stdout then print("Could not open stdout: "..err) return end
stderr,err=io.open("stderr.txt","r")
if not stdout then print("Could not open stderr: "..err) return end
function execute(str)
exe:write(str)
return stdout:read("*all") or stderr:read("*all") or "nil"
end
repeat
print("COMMAND: ")
userinput=io.read("*line")
print("You entered: '"..userinput.."'")
if userinput=="" then print "Empty line! Exiting program..." break end
print("Result: "..execute(userinput))
until true
print "Closing..."
execute("exit")
print "1"
exe:close()
print "2"
stdout:close()
print "3"
stderr:close()
print "Finished!"
Problem: when exiting the program, it hangs on exe:close() call. The execution loop behaves weird too (sometimes I have to press enter several times for userinput=io.read("*line") to work.
I googled to see if file:close() also works on the file handle that is the result of io.popen() but didn't find anything. But that call doesn't fail. It just hangs up. In other words the output of the program is like this:
Command run successfully... ready!
COMMAND:
dir
dir
You entered: 'dirdir'
Result: Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\lua>
C:\lua>
C:\lua>
Closing...
1