我想在Ruby中处理命令行输入:
> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
最好的方法是什么?我特别想处理空白的STDIN,我希望有一个优雅的解决方案。
#!/usr/bin/env ruby
STDIN.read.split("\n").each do |a|
puts a
end
ARGV.each do |b|
puts b
end
我想在Ruby中处理命令行输入:
> cat input.txt | myprog.rb
> myprog.rb < input.txt
> myprog.rb arg1 arg2 arg3 ...
最好的方法是什么?我特别想处理空白的STDIN,我希望有一个优雅的解决方案。
#!/usr/bin/env ruby
STDIN.read.split("\n").each do |a|
puts a
end
ARGV.each do |b|
puts b
end
当前回答
Ruby提供了另一种处理STDIN的方法:-n标志。它将整个程序视为在STDIN上的循环中(包括作为命令行参数传递的文件)。参见以下一行脚本:
#!/usr/bin/env ruby -n
#example.rb
puts "hello: #{$_}" #prepend 'hello:' to each line from STDIN
#these will all work:
# ./example.rb < input.txt
# cat input.txt | ./example.rb
# ./example.rb input.txt
其他回答
似乎大多数答案都假设参数是包含要添加到stdin的内容的文件名。以下的一切都被视为只是论点。如果STDIN来自TTY,那么它将被忽略。
$ cat tstarg.rb
while a=(ARGV.shift or (!STDIN.tty? and STDIN.gets) )
puts a
end
参数或stdin都可以为空或有数据。
$ cat numbers
1
2
3
4
5
$ ./tstarg.rb a b c < numbers
a
b
c
1
2
3
4
5
Ruby提供了另一种处理STDIN的方法:-n标志。它将整个程序视为在STDIN上的循环中(包括作为命令行参数传递的文件)。参见以下一行脚本:
#!/usr/bin/env ruby -n
#example.rb
puts "hello: #{$_}" #prepend 'hello:' to each line from STDIN
#these will all work:
# ./example.rb < input.txt
# cat input.txt | ./example.rb
# ./example.rb input.txt
我要补充一点,为了带参数使用ARGF,你需要在调用ARGF.each之前清除ARGV。这是因为ARGF将ARGV中的任何内容视为文件名,并首先从那里读取行。
这是一个'tee'实现的例子:
File.open(ARGV[0], 'w') do |file|
ARGV.clear
ARGF.each do |line|
puts line
file.write(line)
end
end
简单快捷:
STDIN.gets.chomp == 'YES'
你也可以使用STDIN。each_line和STDIN.each_line。To_a将其作为数组获取。
e.g.
STDIN.each_line do |line|
puts line
end