shell – 未生成文件描述符
我在
shell脚本中遇到问题.我缩小了问题的范围,发现这是因为标准输入的文件描述符不是在proc文件系统中生成的.
下面是我编写的测试脚本: #!/bin/ksh var=`ls -lrt /proc/$$/path/0` echo $var if [[ -f /proc/$$/path/0 ]] then echo "found file descriptor" else echo "file descriptor not found" fi 我在/ tmp /目录中使用示例输入文件对此进行了测试: $./checkip.sh < /tmp/testip lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/19358/path/0 -> /tmp/testip found file descriptor 现在我在我们遇到问题的目录中进行了测试. $./checkip.sh < /var/opt/xxxxxxxx/testip lrwxrwxrwx 1 root root 0 Dec 31 09:15 /proc/20124/path/0 file descriptor not found $ 我想可能是目录xxxxxxxx.所以我再次使用父目录中的文件对此进行了测试. $./checkip.sh < /var/opt/testip lrwxrwxrwx 1 root root 0 Dec 31 09:16 /proc/21286/path/0 -> /var/opt/testip found file descriptor $ 它再次起作用.但我不认为opt和xxxxxxxx的目录权限之间有任何区别 $ls -l |grep xxxxxxxx drwxr-xr-x 292 abcdefg abc 8192 Dec 31 08:33 xxxxxxxx $cd .. $ls -l | grep opt drwxr-xr-x 17 abcdefg abc 512 Dec 31 09:14 opt 我很困惑为什么没有正确创建0(标准输入)的符号链接. 编辑 – 评论 文件不存在 $./checkip.sh < /tmp/notexistfile /tmp/notexistfile: No such file or directory. 文件已存在 $./checkip.sh < /var/opt/testip lrwxrwxrwx 1 root root 0 Dec 31 10:49 /proc/15917/path/0 -> /var/opt/testip found file descriptor 文件已存在 $./checkip.sh < /var/opt/xxxxxxxx/testip lrwxrwxrwx 1 root root 0 Dec 31 10:49 /proc/16566/path/0 file descriptor not found $ 我可以看到的问题是没有为一个目录创建proc / pid / path / 0中标准输入的符号链接.但可能是什么原因……我没有想法!甚至桁架输出也没有显示如何创建文件(sym link)proc / pid / path / 0. 桁架输出:工作案例(检查stat64调用成功): 9107/1: 0.0117 read(62,0xFEF687A0,1024) = 116 9107/1: # ! / b i n / k s hnn i f [ [ - f / p r o c / $$/ p 9107/1: a t h / 0 ] ]n t h e nn e c h o " f o u n d f i l e d 9107/1: e s c r i p t o r "n e l s en e c h o " f i l e d e s c r 9107/1: i p t o r n o t f o u n d "n f in 9107/1: 0.0118 sysconfig(6) = 4096 9107/1: 0.0119 stat64(0x0808E630,0x08089B40) = 0 9107/1: 0x0808E630: "/proc/9107/path/0" 9107/1: d=0x04F40002 i=4191608033 m=0100644 l=1 u=308 g=205 sz=3 9107/1: at = Dec 31 08:33:06 GMT 2014 [ 1420014786.307569314 ] 9107/1: mt = Dec 31 08:33:06 GMT 2014 [ 1420014786.307619723 ] 9107/1: ct = Dec 31 08:33:06 GMT 2014 [ 1420014786.307619723 ] 9107/1: bsz=4096 blks=8 fs=tmpfs truss输出:不工作情况(检查stat64调用失败): 10125/1: 0.0057 read(62,1024) = 116 10125/1: # ! / b i n / k s hnn i f [ [ - f / p r o c / $$/ p 10125/1: a t h / 0 ] ]n t h e nn e c h o " f o u n d f i l e d 10125/1: e s c r i p t o r "n e l s en e c h o " f i l e d e s c r 10125/1: i p t o r n o t f o u n d "n f in 10125/1: 0.0058 sysconfig(6) = 4096 10125/1: 0.0071 stat64(0x0808E630,0x08089B40) Err#2 ENOENT 10125/1: 0x0808E630: "/proc/10125/path/0" 10125/1: 0.0071 lwp_sigmask(3,0x00020000,0x00000000) = 0xFFBFFEFF [0x0000FFFF] 10125/1: 0.0071 stat64(0x0808E792,0x08047600) Err#2 ENOENT 10125/1: 0x0808E792: "/usr/sbin/echo" 10125/1: 0.0072 lwp_sigmask(3,0x00000000,0x00000000) = 0xFFBFFEFF [0x0000FFFF] 10125/1: 0.0072 lwp_sigmask(3,0x00000000) = 0xFFBFFEFF [0x0000FFFF] 解决方法
这取决于你的“if [-f”陈述.它专门检查FILE(-f)是否存在.你在所有例子中得到的是链接,而不是文件.这些链接的解析确定是否满足-f查询.当我在自己的系统上用ls检查/ var结果时,该链接指向一个字符特殊设备,它不会使-f“文件存在”标准饱和.
虽然我对脚本的方向感到困惑.我没有看到它实际上使用您指定的输入.如上所述,“var”完全独立于脚本中.要让-f处理指定的文件名,你必须做一些事情??如果[-f $1](除非您的脚本在发布和示例运行之间发生更改). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |