「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