linux关闭文件描述符及lsof命令

如果你创建了新的输入或输出文件描述符,shell会在脚本退出时自动关闭它们。然而在有些情况下,你需要在脚本结束前手动关闭文件描述符。

要关闭文件描述符,将它重定向到特殊符号&-

举例如下:

[root@localhost shell]# cat test.sh 
#!/bin/bash
exec 3>access.log
echo "This is a test msg" >&3

exec 3>&-
echo "This won't work" >&3
[root@localhost shell]# ./test.sh 
./test.sh:行6: 3: 错误的文件描述符
[root@localhost shell]# cat access.log 
This is a test msg

一旦关闭了文件描述符,就不能在脚本中向它写入任何数据,否则shell会生成错误消息。

不写数据的情况下,如果随后在脚本中打开同一个输出文件,shell会用一个新文件替换已有文件。

[root@localhost shell]# ls
test.sh
[root@localhost shell]# cat test.sh 
#!/bin/bash
exec 3>access.log
echo "This is a test msg" >&3

exec 3>&-
cat access.log

exec 3>access.log
echo "This'll be bad" >&3

[root@localhost shell]# ./test.sh 
This is a test msg
[root@localhost shell]# cat access.log 
This'll be bad

除非使用追加模式(>>

[root@localhost shell]# cat test.sh 
#!/bin/bash
exec 3>access.log
echo "This is a test msg" >&3

exec 3>&-
cat access.log

exec 3>>access.log
echo "This'll be bad" >&3
[root@localhost shell]# ./test.sh 
This is a test msg
[root@localhost shell]# cat access.log 
This is a test msg
This'll be bad

附录:

lsof 命令会列出整个Linux系统打开的所有文件描述符。这是个有争议的功能,因为它会向非系统管理员用户提供Linux系统的信息。鉴于此,许多Linux系统隐藏了该命令。

最常用参数(-p,-d)
前者允许指定进程ID(PID),后者允许指定要显示的文件描述编号

要想知道进程的当前PID,可以用特殊环境变量$$(shell会将它设为当前PID)。-a选项执行布尔AND运算。

在这里插入图片描述

关于TYPE指文件类型
CHR代表字符型,BLK代表块型,DIR代表目录,REG代表常规文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄宝康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值