在脚本中重定向输入和输出,并布局限于以上讲的3个默认的文件描述符,shell最多能够有9个打开的文件描述符。这节介绍在脚本中使用其余文件描述符。linux
Shell 中对文件描述符的操做由三部分组成: (Left, Operation, Right):shell
当存在多个文件描述符的操做时, 会按照从左往右的顺序依次执行. 例如经过命令布局
1 cmd 3>&1 1>&2 2>&3 3>&-
就能够交换 stdin 和 stdout.spa
exec命令用于调用并执行指令的命令。exec命令一般用在shell脚本程序中,能够调用其余的命令。若是在当前终端中使用命令,则当指定的命令执行完毕后会当即退出终端命令行
exec用法:code
lsof会列出整个linux系统打开的全部文件描述符,,命令为隐藏状态。
不少的linux系统中(如Fedora),lsof命令位于/usr/sbinhtm
1 lsof 2 command PID USER FD type DEVICE SIZE NODE NAME 3 init 1 root cwd DIR 8,2 4096 2 / 4 init 1 root rtd DIR 8,2 4096 2 / 5 init 1 root txt REG 8,2 43496 6121706 /sbin/init 6 init 1 root mem REG 8,2 143600 7823908 /lib64/ld-2.5.so 7 init 1 root mem REG 8,2 1722304 7823915 /lib64/libc-2.5.so 8 init 1 root mem REG 8,2 23360 7823919 /lib64/libdl-2.5.so 9 init 1 root mem REG 8,2 95464 7824116 /lib64/libselinux.so.1 10 init 1 root mem REG 8,2 247496 7823947 /lib64/libsepol.so.1 11 init 1 root 10u FIFO 0,17 1233 /dev/initctl 12 migration 2 root cwd DIR 8,2 4096 2 / 13 migration 2 root rtd DIR 8,2 4096 2 / 14 migration 2 root txt unknown /proc/2/exe 15 ksoftirqd 3 root cwd DIR 8,2 4096 2 / 16 ksoftirqd 3 root rtd DIR 8,2 4096 2 / 17 ksoftirqd 3 root txt unknown /proc/3/exe 18 migration 4 root cwd DIR 8,2 4096 2 / 19 migration 4 root rtd DIR 8,2 4096 2 / 20 migration 4 root txt unknown /proc/4/exe 21 ksoftirqd 5 root cwd DIR 8,2 4096 2 / 22 ksoftirqd 5 root rtd DIR 8,2 4096 2 / 23 ksoftirqd 5 root txt unknown /proc/5/exe 24 events/0 6 root cwd DIR 8,2 4096 2 / 25 events/0 6 root rtd DIR 8,2 4096 2 / 26 events/0 6 root txt unknown /proc/6/exe 27 events/1 7 root cwd DIR 8,2 4096 2 /
lsof输出各列信息的意义以下:blog
通常在标准输出、标准错误、标准输入后还跟着文件状态模式:进程
同时在文件状态模式后面,还跟着相关的锁:
tee用于将数据重定向到文件,另外一方面还能够提供一份重定向数据的副本做为后续命令的stdin。简单的说就是把数据重定向到给定文件和屏幕上。