小弟我使用过的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/tty36
crw-rw---- 1 root tty  4, 37 08-01 20:19 /dev/tty37
crw-rw---- 1 root tty  4, 38 08-01 20:19 /dev/tty38
crw-rw---- 1 root tty  4, 39 08-01 20:19 /dev/tty39
crw------- 1 root root 4,  4 08-01 20:23 /dev/tty4
crw-rw---- 1 root tty  4, 40 08-01 20:19 /dev/tty40
crw-rw---- 1 root tty  4, 41 08-01 20:19 /dev/tty41
crw-rw---- 1 root tty  4, 42 08-01 20:19 /dev/tty42
crw-rw---- 1 root tty  4, 43 08-01 20:19 /dev/tty43
crw-rw---- 1 root tty  4, 44 08-01 20:19 /dev/tty44
crw-rw---- 1 root tty  4, 45 08-01 20:19 /dev/tty45
crw-rw---- 1 root tty  4, 46 08-01 20:19 /dev/tty46
crw-rw---- 1 root tty  4, 47 08-01 20:19 /dev/tty47
crw-rw---- 1 root tty  4, 48 08-01 20:19 /dev/tty48
crw-rw---- 1 root tty  4, 49 08-01 20:19 /dev/tty49
crw------- 1 root root 4,  5 08-01 20:23 /dev/tty5
crw-rw---- 1 root tty  4, 50 08-01 20:19 /dev/tty50
crw-rw---- 1 root tty  4, 51 08-01 20:19 /dev/tty51
crw-rw---- 1 root tty  4, 52 08-01 20:19 /dev/tty52
crw-rw---- 1 root tty  4, 53 08-01 20:19 /dev/tty53
crw-rw---- 1 root tty  4, 54 08-01 20:19 /dev/tty54
crw-rw---- 1 root tty  4, 55 08-01 20:19 /dev/tty55
crw-rw---- 1 root tty  4, 56 08-01 20:19 /dev/tty56
crw-rw---- 1 root tty  4, 57 08-01 20:19 /dev/tty57
crw-rw---- 1 root tty  4, 58 08-01 20:19 /dev/tty58
crw-rw---- 1 root tty  4, 59 08-01 20:19 /dev/tty59
crw------- 1 root root 4,  6 08-01 20:23 /dev/tty6
crw-rw---- 1 root tty  4, 60 08-01 20:19 /dev/tty60
crw-rw---- 1 root tty  4, 61 08-01 20:19 /dev/tty61
crw-rw---- 1 root tty  4, 62 08-01 20:19 /dev/tty62
crw-rw---- 1 root tty  4, 63 08-01 20:19 /dev/tty63
crw-rw---- 1 root tty  4,  7 08-01 20:18 /dev/tty7
crw-rw---- 1 root tty  4,  8 08-01 20:18 /dev/tty8
crw-rw---- 1 root tty  4,  9 08-01 20:18 /dev/tty9
crw-rw---- 1 root uucp 4, 64 08-01 20:18 /dev/ttyS0
crw-rw---- 1 root uucp 4, 65 08-01 20:18 /dev/ttyS1
crw-rw---- 1 root uucp 4, 66 08-01 20:18 /dev/ttyS2
crw-rw---- 1 root uucp 4, 67 08-01 20:18 /dev/ttyS3

/dev/pts:
总计 0
crw--w---- 1 root tty 136, 0 11-28 10:37 0
crw--w---- 1 root tty 136, 1 11-28 10:43 1
crw--w---- 1 root tty 136, 7 10-11 23:16 7
[root@web ~]#

 

问题思考

相关资料

【1】中国IT实验室 Linux 指令篇:网络通讯--tty

【2】it群 Linux终端tty设备驱动

【3】ChinaUnix linux tty pty pts 概念 区别

【4】CalmArrow 理解tty

【5】英文资料 Linux解密 The TTY demystified

 

返回 我使用过的Linux命令系列总目录