我有一个像这样的脚本,名为judge
:
#!/bin/bash
echo "last exit status is $?"
它始终输出“最后退出状态为0”。例如:
ls -l; judge # correctly reports 0
ls -z; judge # incorrectly reports 0
beedogs; judge # incorrectly reports 0
为什么?
有不同的bash进程执行每一行代码,$?
并且不会在各个进程之间共享。您可以通过judge
执行bash函数来解决此问题:
[root@xxx httpd]# type judge
judge is a function
judge ()
{
echo "last exit status is $?"
}
[root@xxx httpd]# ls -l / >/dev/null 2>&1; judge
last exit status is 0
[root@xxx httpd]# ls -l /doesntExist >/dev/null 2>&1; judge
last exit status is 2
[root@xxx httpd]#
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句