我的bash里有这样的代码
pkill <stuff>
if [ $? -eq 0 ]; then
echo OK
else
echo FAIL
fi但它总是会进入失败的部分。如何检查pkill命令是否成功?
发布于 2019-02-05 16:37:01
正如手册页中所写的,pkill具有不同的退出状态代码:
EXIT STATUS
0 One or more processes matched the criteria.
1 No processes matched.
2 Syntax error in the command line.
3 Fatal error: out of memory etc.您的代码确实分析了退出代码(这就是$?代表),但您不检查您是否有1、2或3...您应该(!)也请检查此内容:
#!/usr/bin/env bash
pkill <stuff>
pkillexitstatus=$?
if [ $pkillexitstatus -eq 0 ]; then
echo "one or more processes matched the criteria"
elif [ $pkillexitstatus -eq 1]; then
echo "no processes matched"
elif [ $pkillexitstatus -eq 2]; then
echo "syntax error in the command line"
elif [ $pkillexitstatus -eq 3]; then
echo "fatal error"
else
echo UNEXPECTED
fihttps://stackoverflow.com/questions/54530278
复制相似问题