$? 表示啥意思。。。(判断上一个shell语句执行情况)

1730阅读 0评论2007-01-10 枫影谁用了
分类:LINUX

/home/autoupload/bin/rsync -Cavz $LOCALE_DIR/* bb@192.168.2.185::bb/ > $LOG 2>&1
sleep 30

es=$?


if [ $es = 0 ] ;

        then
                echo "Upload successfully." | tee -a $LOG

        else {
                echo "Rsync error! The error code is $es, Please check $LOG_DIR and upload again." | tee -a $LOG
                date | tee -a $LOG
                #kill $!
               }
fi

 

?显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误!


  shell中经常需要判断上一个shell或上一条命令的执行情况,$?即存放此值,一般0表示执行正确,非0表示其他情况。所以exit 后跟一个值退出后,此时根据 $? 就能知道上一个shell执行情况。

假定有两个shell: test1.sh test2.sh,则 test1.sh || test2.sh意思如下: 当test1.sh的返回值为非0 则继续做test2.sh; 当test1.sh的返回值为0时则不做test2.sh。
test1.sh && test2.sh与此相反.


$#
获取参数的个数(0表示沒有参数)

$*
获取全部参数

$@
获取全部参数

$?
获取上一条命令的返回值

上一篇:shell日常要点解到
下一篇:不用注销用另外一个帐户灯录