Ima*_*yne 5 ruby shell command-line stdin popen3
目标:我正在用ruby编写一个工作流命令行程序,该程序在UNIX shell上顺序执行其他程序,其中一些程序需要用户输入输入。
问题:虽然我可以成功地处理stdout,并stderr感谢这个有用的博客文章由尼克·查尔顿,不过,我卡在捕捉用户输入,并将其传递到子过程通过命令行。代码如下:
方法
module CMD
def run(cmd, &block)
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
Thread.new do # STDOUT
until (line = stdout.gets).nil? do
yield nil, line, nil, thread if block_given?
end
end
Thread.new do # STDERR
until (line = stderr.gets).nil? do
yield nil, nil, line, thread if block_given?
end
end
Thread.new do # STDIN
# ????? How to handle
end
thread.join
end
end
end
Run Code Online (Sandbox Code Playgroud)
调用方法
此示例调用shell命令units,该命令提示用户输入度量单位,然后提示您将其转换为单位。这就是它在外壳中的外观
> units
586 units, 56 prefixes # stdout
You have: 1 litre # user input
You want: gallons # user input
* 0.26417205 # stdout
/ 3.7854118 # stdout
Run Code Online (Sandbox Code Playgroud)
当我从程序中运行此程序时,我希望能够以完全相同的方式与其进行交互。
unix_cmd = 'units'
run unix_cmd do | stdin, stdout, stderr, thread|
puts "stdout #{stdout.strip}" if stdout
puts "stderr #{stderr.strip}" if stderr
# I'm unsure how I would allow the user to
# interact with STDIN here?
end
Run Code Online (Sandbox Code Playgroud)
注意:以run这种方式调用方法使用户能够解析输出,控制流程并添加自定义日志记录。
根据我对STDIN的了解,下面的代码段与我了解如何处理STDIN的过程非常接近,由于我仍然不确定如何将其整合到我的run方法中,因此我的知识显然存在一些空白。将输入传递给子进程。
# STDIN: Constant declared in ruby
# stdin: Parameter declared in Open3.popen3
Thread.new do
# Read each line from the console
STDIN.each_line do |line|
puts "STDIN: #{line}" # print captured input
stdin.write line # write input into stdin
stdin.sync # sync the input into the sub process
break if line == "\n"
end
end
Run Code Online (Sandbox Code Playgroud)
简介:我希望了解如何通过该Open3.popen3方法从命令行处理用户输入,以便允许用户将数据输入到从我的程序中调用的各种子命令序列中。
经过大量阅读有关 STDIN 以及一些良好的旧试验和错误之后,我发现一个实现与Charles Finkel 的 答案没有什么不同,但有一些细微的差异。
require "open3"
module Cmd
def run(cmd, &block)
Open3.popen3(cmd) do |stdin, stdout, stderr, thread|
# We only need to check if the block is provided once
# rather than every cycle of the loop as we were doing
# in the original question.
if block_given?
Thread.new do
until (line = stdout.gets).nil? do
yield line, nil, thread
end
end
Thread.new do
until (line = stderr.gets).nil? do
yield nil, line, thread
end
end
end
# $stdin.gets reads from the console
#
# stdin.puts writes to child process
#
# while thread.alive? means that we keep on
# reading input until the child process ends
Thread.new do
stdin.puts $stdin.gets while thread.alive?
end
thread.join
end
end
end
include Cmd
Run Code Online (Sandbox Code Playgroud)
像这样调用该方法:
run './test_script.sh' do | stdout, stderr, thread|
puts "#{thread.pid} stdout: #{stdout}" if stdout
puts "#{thread.pid} stderr: #{stderr}" if stderr
end
Run Code Online (Sandbox Code Playgroud)
哪里test_script.sh如下:
echo "Message to STDOUT"
>&2 echo "Message to STDERR"
echo "enter username: "
read username
echo "enter a greeting"
read greeting
echo "$greeting $username"
exit 0
Run Code Online (Sandbox Code Playgroud)
产生以下成功输出:
25380 stdout: Message to STDOUT
25380 stdout: enter username:
25380 stderr: Message to STDERR
> Wayne
25380 stdout: enter a greeting
> Hello
25380 stdout: Hello Wayne
Run Code Online (Sandbox Code Playgroud)
注意:您会注意到 stdout 和 stderr 没有按顺序出现,这是我尚未解决的限制。
如果您有兴趣了解有关 stdin 的更多信息,则值得阅读以下问题的答案 - What is the Difference Between STDIN and $stdin in Ruby?