How can I tell if STDIN is connected to a terminal in Perl?

◇◆丶佛笑我妖孽 提交于 2019-12-05 21:30:30

问题


How can I tell if STDIN is connected to a terminal in Perl?


回答1:


if (-t STDIN) {
  # stdin is connected
} else {
  # stdin is not connected
}

I usually use this in conjunction with -t STDOUT, to find out if I'm running from an interactive shell, or from cron, to enable more output.




回答2:


You might also be interested in IO::Interactive to figure out if Perl thinks it is interacting with a user. Simply being connected to a tty doesn't mean the user is going to see what you do.




回答3:


One solution would be to use tty:

[root@server] ~> tty
/dev/pts/0
[root@server] ~> echo y | tty
not a tty

But not very pretty...



来源:https://stackoverflow.com/questions/528781/how-can-i-tell-if-stdin-is-connected-to-a-terminal-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!