Linux:在 Bash 脚本中读取 readlink /proc/pid/exe 的输出
Linux: Reading the output of readlink /proc/pid/exe within a Bash Script
所以我正在编写一个 bash 脚本,它将 运行 通过 /proc/[pid] 中的所有进程 ID 并读取用于 运行 它的可执行文件.
根据我的观察,/proc 文件系统包含 /proc/[pid]/exe 符号 link。在 bash 脚本中,我正在尝试找出如何读取 "readlink /proc/[pid]/exe" 的值以检查是否(已删除)或没有返回任何内容以查明原始可执行文件是否存在于磁盘上。
有没有办法做到这一点,到目前为止我有?
#!/bin/bash
pid = "0"
while [ $pid -lt 32769 ]
do
if [-d /proc/$pid]; then
if [-f /proc/$pid/exe]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid = $[$pid+1]
done
这无法工作并且总是 returns nothing.I 我正在尝试列出磁盘上不再有其可执行文件的所有进程。
我已经更新了您的脚本以使其工作。请注意 -f 检查文件名是否表示 常规 文件。对于符号 link:
我会 return false
pid="0"
while [ $pid -lt 32769 ]
do
if [ -d /proc/$pid ]; then
if [ -h /proc/$pid/exe ]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid=$[$pid+1]
done
您可以通过打印 $? 在 shell 中的任何命令后读取返回值变量:
readlink
echo $?
如果link无效,$?将大于 0.
但是如果 link 存在并且实际文件被删除,您可以使用类似的东西:
ls `readlink somelink`
这对你有用吗?
#!/bin/bash
for i in $(ls /proc | awk '/^[[:digit:]]+/{print }'); do
if [ -h /proc/$i/exe ]; then
echo -n "$i: "
if readlink /proc/$i/exe >/dev/null 2>&1 ; then
echo "executable exists"
else
echo "executable not found"
fi
fi
done
readlink -f `ls --dereference /proc/$pid/exe`
所以我正在编写一个 bash 脚本,它将 运行 通过 /proc/[pid] 中的所有进程 ID 并读取用于 运行 它的可执行文件.
根据我的观察,/proc 文件系统包含 /proc/[pid]/exe 符号 link。在 bash 脚本中,我正在尝试找出如何读取 "readlink /proc/[pid]/exe" 的值以检查是否(已删除)或没有返回任何内容以查明原始可执行文件是否存在于磁盘上。
有没有办法做到这一点,到目前为止我有?
#!/bin/bash
pid = "0"
while [ $pid -lt 32769 ]
do
if [-d /proc/$pid]; then
if [-f /proc/$pid/exe]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid = $[$pid+1]
done
这无法工作并且总是 returns nothing.I 我正在尝试列出磁盘上不再有其可执行文件的所有进程。
我已经更新了您的脚本以使其工作。请注意 -f 检查文件名是否表示 常规 文件。对于符号 link:
我会 return falsepid="0"
while [ $pid -lt 32769 ]
do
if [ -d /proc/$pid ]; then
if [ -h /proc/$pid/exe ]; then
echo $pid
readlink /proc/$pid/exe
fi
fi
pid=$[$pid+1]
done
您可以通过打印 $? 在 shell 中的任何命令后读取返回值变量:
readlink
echo $?
如果link无效,$?将大于 0.
但是如果 link 存在并且实际文件被删除,您可以使用类似的东西:
ls `readlink somelink`
这对你有用吗?
#!/bin/bash
for i in $(ls /proc | awk '/^[[:digit:]]+/{print }'); do
if [ -h /proc/$i/exe ]; then
echo -n "$i: "
if readlink /proc/$i/exe >/dev/null 2>&1 ; then
echo "executable exists"
else
echo "executable not found"
fi
fi
done
readlink -f `ls --dereference /proc/$pid/exe`