ターミナルからパイプで渡された文字列群があるかどうかを確認するRubyサンプルコードを以下に記します。
以下、サンプルコードおよび実行例を記します。
#!/usr/bin/env ruby
def isPipe()
if File.pipe?(STDIN) || File.select([STDIN], [], [], 0) != nil then
return true
end
false
end
if isPipe() then
puts $stdin.gets();
else
puts "nothing"
end
以下に本サンプルを実行したときの出力を記します。
sakura@debian:~$ ruby -v ruby 2.1.5p273 (2014-11-13) [x86_64-linux-gnu]
sakura@debian:~$ chmod +x ispipe.rb sakura@debian:~$ ./ispipe.rb nothing sakura@debian:~$ echo SAKURA | ./ispipe.rb SAKURA
以上、Rubyでパイプ渡しのstdinの有無を確認する方法でした。