当前位置: 首页>>技术教程>>正文


bash – set -e 的作用是什么?

问题描述

我找到了一个脚本,我试图让它运行,它只是下载并安装 Apache 运行时库。第一行是 set -e,它导致错误; set -e 是做什么的?

最佳办法

\\n

This builtin is so complicated that it deserves its own section.

\\n

set\\n allows you to change the values of shell options and set the\\n positional parameters, or to display the names and values of shell\\n variables.

\\n

-e 选项

\\n

-e

\\n

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

\\n

This option applies to the shell environment and each subshell\\n environment separately (see Command Execution Environment), and may\\n cause subshells to exit before executing all the commands in the\\n subshell.

\\n

来源:www.gnu.org

根据下面@psusi 的评论进行编辑。

另外你可以阅读 bash 的手册页

man bash 

在部分:SHELL 内置命令

或问题

help set 

获取简短的帮助消息。

次佳办法

简而言之,set -e 位于 bash 脚本的顶部时,告诉脚本在脚本中的任何行失败时立即退出(手册中列出了一些例外情况。)[1]

作为调试选项,它通常与 set -x 一起使用,即在执行之前使用 “+” 符号打印每个扩展的命令行。 [2]

更多信息请访问:

[1] https://www.peterbe.com/plog/set-ex

[2] http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/14257.html,未经允许,请勿转载。