How can you check for STDIN input in a Ruby script?

后端 未结 1 1449
有刺的猬
有刺的猬 2020-12-30 02:27

I need to check for the presence of STDIN input in a Ruby script, like the mysql command can. If nothing is being directed to STDIN, then the script should not

相关标签:
1条回答
  • 2020-12-30 03:04

    This is something that's done in Linux a lot:

    #!/usr/bin/env ruby
    
    str = (STDIN.tty?) ? 'not reading from stdin' : $stdin.read
    puts str
    
    >> $ ruby test.rb 
    >> not reading from stdin
    >> $ echo "reading from stdin" | ruby test.rb 
    >> reading from stdin
    
    0 讨论(0)
提交回复
热议问题