如何判断STDIN是否连接到Perl中的终端?

时间:2022-06-11 00:04:01

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

如何判断STDIN是否连接到Perl中的终端?

3 个解决方案

#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.

我通常将它与-t STDOUT结合使用,以确定我是从交互式shell还是从cron运行,以启用更多输出。

#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.

您可能还对IO :: Interactive感兴趣,以确定Perl是否认为它与用户进行交互。简单地连接到tty并不意味着用户将会看到你做的事情。

#3


One solution would be to use tty:

一种解决方案是使用tty:

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

But not very pretty...

但不是很漂亮......

#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.

我通常将它与-t STDOUT结合使用,以确定我是从交互式shell还是从cron运行,以启用更多输出。

#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.

您可能还对IO :: Interactive感兴趣,以确定Perl是否认为它与用户进行交互。简单地连接到tty并不意味着用户将会看到你做的事情。

#3


One solution would be to use tty:

一种解决方案是使用tty:

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

But not very pretty...

但不是很漂亮......