新手问题 unicorn.sh 问题请教

xiaoronglv · 2014年12月14日 · 最后由 xiaoronglv 回复于 2014年12月17日 · 1908 次阅读

这是我在网上抄的一段部署脚本,bash 脚本的头部已经声明了 set -e。只要任何一行代码的 exit code > 0,整个脚本就会停止执行并退出。

如果 $APP_ROOT 不存在, cd $APP_ROOT 会让整个脚本中断执行。所以照我的理解,后面的代码中的|| exit 1,是不是有点多余?

抑或 exit 1 在这段脚本中,有特殊的用意?

1 楼 已删除

我写 init.d 得出的经验是不要写 init.d http://chloerei.com/2014/12/15/foreman/

不过这不能解决楼主的问题。

#1 楼 @xiaoronglv 我觉得应该是多余的,从定义上来看。

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a subshell command enclosed in parentheses (see Command Grouping), or one of the commands executed as part of a command list enclosed by braces (see Command Grouping) returns a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command’s return status is being inverted with !. A trap on ERR, if set, is executed before the shell exits.

This option applies to the shell environment and each subshell environment separately (see Command Execution Environment), and may cause subshells to exit before executing all the commands in the subshell.
4 楼 已删除
  • sh 的 cd 错误是 exit code 是 2
  • bash 的 cd 错误 exit code 是 1

这个脚本的作者通过 cd $APP_ROOT || exit 1 把错误从 2 改为 1

荣耀归给 @zgm

#3 楼 @serco

作者把 exit code 从 2 改为 1,貌似意义不大。就是 debug 时方便一些。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号