“Answering a cli prompt in ruby with open3?”是一个可能重复的问题,但它没有答案。模拟命令提示符命令
我需要编写一个程序来编译和执行一个C程序,给出输入并返回输出。到目前为止,我想出了这一点:
对于单输入:
Open3.popen3('one.exe') do |stdin, stdout, stderr|
stdin.puts "45\n"
STDOUT.puts stdout.gets
end
,输出是:
Enter the temperature in degrees fahrenheit: The converted temperature is 7.222222
对于两个或多个输入:
Open3.popen3('two.exe') do |stdin, stdout, stderr|
stdin.puts "45 45"
# This line works the same as the previous one.
# stdin.puts "45\r\n45"
stdin.close
STDOUT.puts stdout.gets
end
和输出为:
Enter first number: Enter second number: Sum is 90
的问题是,我没有回去我放在输入。
有没有一种方法,我可以纠正或者一个更好的方式来做到这一点?
感谢您的回复。我尝试了上面的例子,它们按预期工作,所以我跳回到我原来的任务,** temp.rb **的内容:'puts%x [sh ./test.sh