小弟我使用过的Linux命令之tty – 打印标准输入的文件名称

小弟我使用过的Linux命令之tty - 打印标准输入的文件名称

我使用过的Linux命令之tty – 打印标准输入的文件名称

我使用过的Linux命令之tty – 打印标准输入的文件名称

本文链接:http://codingstandards.iteye.com/blog/826759
?? (转载请注明出处)

用途说明

tty命令用于打印标准输入的文件名称(print the file name of the terminal connected to standard input,打印与标准输入连接的终端设备的文件名称)。在Linux系统中,终端是一种字符型设备,它有多种类型,通常使用tty来简称各种类型的终端设备。tty是Teletype的缩写,Teletype是最早出现的一种终端设备,很像电传打字机,是由Teletype公司生产的。在Linux系统中,终端设备非常重要,没有终端设备,系统将无法向用户反馈信息,Linux中包含控制台、串口和伪终端3类终端设备。本文后面提供很多相关资料,很值得一看。

常用参数

  tty命令不跟任何参数,会打印标准输入设备的文件名称,如果标准输入不是终端设备时,打印“not a tty”,否则打印终端设备名称,比如“/etc/tty1”、“/etc/pts/1”。(`tty’ prints the file name of the terminal connected to its standard
input.? It prints `not a tty’ if standard input is not a terminal.)

  tty命令跟上参数-s(或–silent或–quiet),不会打印任何信息,根据退出状态可以用来判断标准输入是否是终端。(Print nothing; only return an exit status.)
  tty命令的退出状态:如果标准输入是终端时为0,否则返回1。(Exit status: 0 if standard input is a terminal,1 if standard input is not a terminal,2 if given incorrect arguments,3 if a write error occurs)

使用示例

示例一 在控制台终端上执行tty

[root@localhost data]# tty

/dev/tty1

[root@localhost data]#

?

示例二 在伪终端上执行tty

[root@web ~]# tty

/dev/pts/1
[root@web ~]#

?

示例三 在脚本中判断

#!/bin/sh

if tty -s; then
        echo "it's a terminal"
fi

if [ "$(tty)" != "not a tty" ]; then
        echo "it's a terminal"
fi

?

[root@web ~]# cat tty.sh

#!/bin/sh

if tty -s; then
??????? echo “it’s a terminal”
fi

if [ “$(tty)” != “not a tty” ]; then
??????? echo “it’s a terminal”
fi

[root@web ~]# ./tty.sh

it’s a terminal
it’s a terminal
[root@web ~]# ./tty.sh < /dev/null

[root@web ~]# ./tty.sh < ./tty.sh

[root@web ~]#

?

示例四 显示系统中的终端设备

[root@web ~]# ls -l /dev/console /dev/tty* /dev/pts*

crw——- 1 root root 5,? 1 11-28 10:42 /dev/console
crw-rw-rw- 1 root tty? 5,? 0 11-22 17:21 /dev/tty
crw-rw—- 1 root tty? 4,? 0 11-28 10:37 /dev/tty0
crw——- 1 root root 4,? 1 08-01 20:23 /dev/tty1
crw-rw—- 1 root tty? 4, 10 08-01 20:18 /dev/tty10
crw-rw—- 1 root tty? 4, 11 08-01 20:18 /dev/tty11
crw-rw—- 1 root tty? 4, 12 08-01 20:18 /dev/tty12
crw-rw—- 1 root tty? 4, 13 08-01 20:19 /dev/tty13
crw-rw—- 1 root tty? 4, 14 08-01 20:19 /dev/tty14
crw-rw—- 1 root tty? 4, 15 08-01 20:19 /dev/tty15
crw-rw—- 1 root tty? 4, 16 08-01 20:19 /dev/tty16
crw-rw—- 1 root tty? 4, 17 08-01 20:19 /dev/tty17
crw-rw—- 1 root tty? 4, 18 08-01 20:19 /dev/tty18
crw-rw—- 1 root tty? 4, 19 08-01 20:19 /dev/tty19
crw——- 1 root root 4,? 2 08-01 20:23 /dev/tty2
crw-rw—- 1 root tty? 4, 20 08-01 20:19 /dev/tty20
crw-rw—- 1 root tty? 4, 21 08-01 20:19 /dev/tty21
crw-rw—- 1 root tty? 4, 22 08-01 20:19 /dev/tty22
crw-rw—- 1 root tty? 4, 23 08-01 20:19 /dev/tty23
crw-rw—- 1 root tty? 4, 24 08-01 20:19 /dev/tty24
crw-rw—- 1 root tty? 4, 25 08-01 20:19 /dev/tty25
crw-rw—- 1 root tty? 4, 26 08-01 20:19 /dev/tty26
crw-rw—- 1 root tty? 4, 27 08-01 20:19 /dev/tty27
crw-rw—- 1 root tty? 4, 28 08-01 20:19 /dev/tty28
crw-rw—- 1 root tty? 4, 29 08-01 20:19 /dev/tty29
crw——- 1 root root 4,? 3 08-01 20:23 /dev/tty3
crw-rw—- 1 root tty? 4, 30 08-01 20:19 /dev/tty30
crw-rw—- 1 root tty? 4, 31 08-01 20:19 /dev/tty31
crw-rw—- 1 root tty? 4, 32 08-01 20:19 /dev/tty32
crw-rw—- 1 root tty? 4, 33 08-01 20:19 /dev/tty33
crw-rw—- 1 root tty? 4, 34 08-01 20:19 /dev/tty34
crw-rw—- 1 root tty? 4, 35 08-01 20:19 /dev/tty35
crw-rw—- 1 root tty? 4, 36 08-01 20:19 /dev

小弟我使用过的Linux命令之tty – 打印标准输入的文件名称

相关文章:

你感兴趣的文章:

标签云: