shell - 未生成文件描述符

标签 shell unix solaris ksh

我在 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
$

我想可能是目录 xxxxxxxxx。所以我用父目录中的一个文件再次测试了这个。
$ ./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(标准输入)的符号链接(symbolic link)感到困惑。
任何人都可以帮我找出原因吗?

编辑 - 评论

文件不存在
$ ./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 中标准输入的符号链接(symbolic link)没有为一个目录创建。但可能是什么原因......我没有想法!即使是 truss 输出也没有显示任何关于如何创建文件(符号链接(symbolic link))proc/pid/path/0 的信息。

桁架输出:工作案例(检查 stat64 调用成功):
9107/1:          0.0117 read(62, 0xFEF687A0, 1024)                      = 116
9107/1:            # ! / b i n / k s h\n\n i f   [ [     - f   / p r o c / $ $ / p
9107/1:            a t h / 0   ] ]\n t h e n\n 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 e\n 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 i\n
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, 0xFEF687A0, 1024)                      = 116
10125/1:           # ! / b i n / k s h\n\n i f   [ [     - f   / p r o c / $ $ / p
10125/1:           a t h / 0   ] ]\n t h e n\n 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 e\n 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 i\n
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, 0x00020000, 0x00000000)          = 0xFFBFFEFF [0x0000FFFF]

最佳答案

这取决于您的“if [-f”语句。它专门检查 FILE (-f) 是否存在。您在所有示例中获得的是链接,而不是文件。这些链接的解析决定了是否满足 -f 查询。当我在自己的系统上使用 ls 检查/var 结果时,链接指向一个字符特殊设备,它不会满足 -f“文件存在”标准。

不过,我对脚本的方向感到困惑。我没有看到它实际使用您指定的输入的任何地方。正如您在上面指定的那样,“var”在脚本中完全是独立的。要让 -f 在指定的文件名上工作,您必须按照以下方式进行操作
如果 [ -f $1 ]
(除非您的脚本在发布和示例运行之间发生变化)。

关于shell - 未生成文件描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27719173/

相关文章:

php - 在 UNIX/linux 服务器中自动运行 shell 脚本

linux - grep 一个子串

java - 禁用 PKCS11Solaris 实现

java - 通过java程序进行rsync-远程连接失败的问题

objective-c - OSX 以 root 身份以编程方式启动 launchctl

bash - 读取stdout时如何保留shell脚本行距?

linux - 使用 linux 管道连接字符串

java - 将 equinox OSGi 作为 Fedora 服务运行

bash - awk:如何从文件 A 中提取文件 B 中指定索引的列?

python - 在 Solaris 上使用 Python 终止进程时出现问题