如何检测Ruby脚本是否通过外壳管道运行? [英] How to detect if a Ruby script is running through a shell pipe?
本文介绍了如何检测Ruby脚本是否通过外壳管道运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
My question is similar to this one: How to detect if my shell script is running through a pipe?. The difference is that the script I’m working on is written in Ruby.
假设我跑步:
./test.rb
我希望标准输出上的文本带有颜色,但是
I expect text on stdout with color, but
./test.rb | cat
我希望颜色代码被剥离.
I expect the color codes to be stripped out.
推荐答案
习惯上使用$stdout.isatty
或更高版本,$stdout.tty?
.我创建了一个小的test.rb文件来演示内容:
Use $stdout.isatty
or more idiomatically, $stdout.tty?
. I created a little test.rb file to demonstrate, contents:
puts $stdout.isatty
结果:
$ ruby test.rb
true
$ ruby test.rb | cat
false
参考: https://ruby-doc.org/core/IO.html#method-i-isatty
这篇关于如何检测Ruby脚本是否通过外壳管道运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文