$? 上个bai命令的退出状态,或函du数的返回值。
例子1 :
ls 命令没zhi有找到匹配的结果. 所以返回2 $? 就是dao2
[root@sg-rhel6-17 etc]# ls /tmp/*.log
ls: cannot access /tmp/*.log: No such file or directory
[root@sg-rhel6-17 etc]# echo $?
2
例子2 :
ls 命令找到了结果. 成功返回0 所以$? 就是0
[root@sg-rhel6-17 etc]# ls /tmp/*.tmp
/tmp/reminder.tmp
[root@sg-rhel6-17 etc]# echo $?
0
多指执行bai命令,一般在程序里面du会对其进行判断,如果发现上一行的命令执行有误,该脚本就会终止执行。
if [ $? -ne 0 ]
then
#脚本不正常退出
exit 1
fi