in shell, there is a special variable $?, which catchthe status of last command:
after "ls", "echo $?" will return 0, meaning a successful command;
after "xxxxxx", "echo $?" will be a non-zero, since xxxxxx is not
an command.
Now, in the above script, exit 0 and exit 1 is up to your choice.
but you still can use $? to catch that value, since Shell treat
"if ....;then .....fi" as a command
---------------------------------------------------------------------------------------------------------------------------------------------------
当你 exit0 的时候
在调用环境echo $? 就返回0 ,也就是说调用环境就认为 你的这个程序执行正确
当 exit 1 的时候,一般是出错定义这个1,也可以是其他数字,很多系统程序这个错误编号是有约定的含义的。 但不为0就表示程序运行出错。调用环境就可以根据这个返回值判断 你这个程序运行是否ok。
如果你用 脚本 a调用 脚本b ,要在a中判断b是否正常返回,就是根据exit 0 or1 来识别。
执行完b后, 判断$? 就是返回值
本文介绍Shell脚本中的特殊变量$?的作用及其应用场景。$?用于捕获上一条命令的状态,通过其返回值(0表示成功,非0表示失败)来判断命令执行情况。在脚本开发中,利用$?可以实现对子脚本执行状态的检查。
952

被折叠的 条评论
为什么被折叠?



