linux终端文件描述符,自学Linux Shell14.2-在脚本中使用其他文件描述符

14.2-在脚本中使用其他文件描述符

在脚本中重定向输入和输出,并布局限于以上讲的3个默认的文件描述符,shell最多可以有9个打开的文件描述符。这节介绍在脚本中使用其他文件描述符。

1.文件描述符的操作

Shell 中对文件描述符的操作由三部分组成:(Left, Operation, Right):Left 可以是 0-9 的数字, 代表第 n 号文件描述符;

Left 还可以为 &, 表示同时操作 stdout 和 stderr

Right 可以是文件名或 0-9 的数字, 当 Right 是数字时必须要加上 & 符号, 表示引用第 n 号文件描述符;

Right 还可以为 &-, 此时表示关闭 Left 描述符, 例如 2

Operation 可以为 < 或 >;

为 < 时表示以读模式复制 Right 到 Left, 此时如果没有指定 Left 的话, 则为默认值 0;

当为 > 表示以写模式复制 Right 到 Left, 此时如果没有指定 Left 的话, 则为默认值 1;

Operation 和 Left 之间不能有空格;

当 Right 为文件名时, Operation 和 Right 可以有空格, 否则也不能有空格;

当存在多个文件描述符的操作时, 会按照从左往右的顺序依次执行. 例如通过命令

cmd >& >& >& >&-

就可以交换 stdin 和 stdout.

2 I/O重定向的常用用法command > filename      把标准输出重定向到一个新文件中

command >> filename      把标准输出重定向到一个文件中(追加)

command 1 > fielname      把标准输出重定向到一个文件中

command > filename 2>&1    把标准输出和标准错误一起重定向到一个文件中

command 2 > filename     把标准错误重定向到一个文件中

command 2 >> filename     把标准输出重定向到一个文件中(追加)

command >> filename 2>&1   把标准输出和标准错误一起重定向到一个文件中(追加)

command < filename >filename2   把command命令以filename文件作为标准输入,以filename2文件作为标准输出

command < filename    把command命令以filename文件作为标准输入

command << delimiter   把从标准输入中读入,直至遇到delimiter分界符

command

command >&m    把标准输出重定向到文件描述符m中

command

command %n%c file    command 输出重定向的命令 ,%n 文件描述符,默认值为1,%c 重定向符 ,file 目标文件。

3. exec命令的介绍

exec命令用于调用并执行指令的命令。exec命令通常用在shell脚本程序中,可以调用其他的命令。如果在当前终端中使用命令,则当指定的命令执行完毕后会立即退出终端

exec用法:&n :代表描述符代表的文件

> < :代表以什么形式使用描述符

exec 8

exec &>log:把标准输入错误打开文件log

exec 8

4. 列出打开的文件描述符lsof

lsof会列出整个linux系统打开的所有文件描述符,,命令为隐藏状态。

很多的linux系统中(如Fedora),lsof命令位于/usr/sbin

lsof

command PID USER FD type DEVICE SIZE NODE NAME

init root cwd DIR , /

init root rtd DIR , /

init root txt REG , /sbin/init

init root mem REG , /lib64/ld-2.5.so

init root mem REG , /lib64/libc-2.5.so

init root mem REG , /lib64/libdl-2.5.so

init root mem REG , /lib64/libselinux.so.

init root mem REG , /lib64/libsepol.so.

init root 10u FIFO , /dev/initctl

migration root cwd DIR , /

migration root rtd DIR , /

migration root txt unknown /proc//exe

ksoftirqd root cwd DIR , /

ksoftirqd root rtd DIR , /

ksoftirqd root txt unknown /proc//exe

migration root cwd DIR , /

migration root rtd DIR , /

migration root txt unknown /proc//exe

ksoftirqd root cwd DIR , /

ksoftirqd root rtd DIR , /

ksoftirqd root txt unknown /proc//exe

events/ root cwd DIR , /

events/ root rtd DIR , /

events/ root txt unknown /proc//exe

events/ root cwd DIR , /

lsof输出各列信息的意义如下:COMMAND:进程的名称

PID:进程标识符

USER:进程所有者

FD:文件描述符,应用程序通过文件描述符识别该文件

TYPE:文件的类型,DIR表示字符型,BLK表示块型,DIR表示目录,REG表示常规文件

DEVICE:设备的设备号

SIZE:如果有的话,表示文件的大小

NODE:本地文件的节点号

NAME:文件名

一般在标准输出、标准错误、标准输入后还跟着文件状态模式:u:表示该文件被打开并处于读取/写入模式。

r:表示该文件被打开并处于只读模式。

w:表示该文件被打开并处于。

空格:表示该文件的状态模式为unknow,且没有锁定。

-:表示该文件的状态模式为unknow,且被锁定。

同时在文件状态模式后面,还跟着相关的锁:N:for a Solaris NFS lock of unknown type;

r:for read lock on part of the file;

R:for a read lock on the entire file;

w:for a write lock on part of the file;(文件的部分写锁)

W:for a write lock on the entire file;(整个文件的写锁)

u:for a read and write lock of any length;

U:for a lock of unknown type;

x:for an SCO OpenServer Xenix lock on part      of the file;

X:for an SCO OpenServer Xenix lock on the      entire file;

space:if there is no lock.

5. 记录消息tee

tee用于将数据重定向到文件,另一方面还可以提供一份重定向数据的副本作为后续命令的stdin。简单的说就是把数据重定向到给定文件和屏幕上。默认情况下,tee命令在每次使用时候覆盖输出文件内容

-a , 将数据追加到文件中

-i ,忽略终端信号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值