简体   繁体   English

bash 中使用的 -ex 选项有什么作用? #!/bin/bash -ex 意思是

[英]What does -ex option used in bash | #!/bin/bash -ex mean

We are using the script below as user data for EC2 instances.我们使用下面的脚本作为 EC2 实例的用户数据。 I don't understand what is -ex option is being used for?我不明白-ex选项的用途是什么?

#!/bin/bash -ex

yum update -y
yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
service httpd start
chkconfig httpd on

According to Add tack ex on your bash shebang |根据在你的 bash shebang 上添加 tack ex | #!/bin/bash -ex

Bash scripts can use various options on the shebang (#!/bin/bash). Bash 脚本可以使用 shebang (#!/bin/bash) 上的各种选项。 A more common one is: '#!/bin/bash -ex'.更常见的是:'#!/bin/bash -ex'。

-e Exit immediately if a command exits with a non-zero status. -e如果命令以非零状态退出,则立即退出。
-x Print commands and their arguments as they are executed. -x在执行时打印命令及其参数。

In short, adding -ex to your #!/bin/bash will give verbose output and also will abort your script immediately if part of the script fails.简而言之,将-ex添加到您的#!/bin/bash将提供详细的输出,并且如果脚本的一部分失败,也会立即中止您的脚本。

This is just another way of saying :这只是另一种说法:

#!/bin/bash 

set -ex # Added line

yum update -y
yum groupinstall -y "Web Server" "MySQL Database" "PHP Support"
service httpd start
chkconfig httpd on

[ bash manual ] says : [bash 手册]说:

All of the single-character options used with the set builtin (see The Set Builtin) can be used as options when the shell is invoked当调用 shell 时,所有与 set 内置命令一起使用的单字符选项(请参阅 The Set Builtin)都可以用作选项

[ set reference ] says : [设置参考]说:

set allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. set 允许您更改 shell 选项的值和设置位置参数,或显示 shell 变量的名称和值。

-x -x

Print a trace of simple commands, for commands, case commands, select commands, and arithmetic for commands and their arguments or associated word lists after they are expanded and before they are executed.在扩展之后和执行之前,打印简单命令的踪迹,用于命令、case 命令、选择命令和命令及其参数的算术或关联的单词列表。 The value of the PS4 variable is expanded and the resultant value is printed before the command and its expanded arguments. PS4 变量的值被扩展,结果值打印在命令及其扩展参数之前。

-e -e

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists), or a compound command (see Compound Commands) returns a non-zero status.如果管道(请参阅管道)(可能由单个简单命令(请参阅简单命令)、列表(请参阅列表)或复合命令(请参阅复合命令)组成)返回非零状态,则立即退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM