现在的位置: 首页 > 综合 > 正文

SHELL中的exit 0 和 exit 1的区别

2013年10月17日 ⁄ 综合 ⁄ 共 693字 ⁄ 字号 评论关闭

in shell, there is a special variable $?, which catch  the 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

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------

 

当你 exit  0 的时候
在调用环境  echo $?    就返回0   ,也就是说调用环境就认为 你的这个程序执行正确

当 exit 1 的时候,一般是出错定义这个1,也可以是其他数字,很多系统程序这个错误编号是有约定的含义的。 但不为0  就表示程序运行出错。  调用环境就可以根据这个返回值判断 你这个程序运行是否ok。

如果你用 脚本 a  调用 脚本b ,要在a中判断b是否正常返回,就是根据  exit 0   or  1 来识别。
执行完b后, 判断  $?   就是返回值

【上篇】
【下篇】

抱歉!评论已关闭.