bash中使用了什么-ex选项#!/ bin / bash -ex mean

时间:2021-09-05 00:29:24

We are using the script below as user data for EC2 instances. I don't understand what is -ex option is being used for?

我们使用下面的脚本作为EC2实例的用户数据。我不明白什么是-ex选项正在使用?

#!/bin/bash -ex

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

2 个解决方案

#1


7  

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

与set builtin一起使用的所有单字符选项(请参阅Set Builtin)可以在调用shell时用作选项

[ 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. 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.

如果管道(请参阅管道)(可能包含单个简单命令(请参阅简单命令)),列表(请参阅列表)或复合命令(请参阅复合命令)返回非零状态,请立即退出。

#2


2  

A nice post that answers the question: "Add tack e x on your bash shebang | #!/bin/bash -ex" https://scottlinux.com/2014/08/05/add-tack-e-x-on-your-bash-shebang-binbash-ex/

一个很好的帖子回答了这个问题:“在你的bash shebang上添加大头钉|#!/ bin / bash -ex”https://scottlinux.com/2014/08/05/add-tack-ex-on-your-庆典 - 家当 - binbash-EX /

Bash scripts can use various options on the shebang (#!/bin/bash). A more common one is: ‘#!/bin/bash -ex’.

Bash脚本可以使用shebang上的各种选项(#!/ bin / bash)。更常见的是:'#!/ bin / bash -ex'。

-e Exit immediately if a command exits with a non-zero status. -x Print commands and their arguments as they are executed.

-e如果命令以非零状态退出,则立即退出。 -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将提供详细输出,并且如果脚本的一部分失败,也会立即中止脚本。

#1


7  

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

与set builtin一起使用的所有单字符选项(请参阅Set Builtin)可以在调用shell时用作选项

[ 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. 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.

如果管道(请参阅管道)(可能包含单个简单命令(请参阅简单命令)),列表(请参阅列表)或复合命令(请参阅复合命令)返回非零状态,请立即退出。

#2


2  

A nice post that answers the question: "Add tack e x on your bash shebang | #!/bin/bash -ex" https://scottlinux.com/2014/08/05/add-tack-e-x-on-your-bash-shebang-binbash-ex/

一个很好的帖子回答了这个问题:“在你的bash shebang上添加大头钉|#!/ bin / bash -ex”https://scottlinux.com/2014/08/05/add-tack-ex-on-your-庆典 - 家当 - binbash-EX /

Bash scripts can use various options on the shebang (#!/bin/bash). A more common one is: ‘#!/bin/bash -ex’.

Bash脚本可以使用shebang上的各种选项(#!/ bin / bash)。更常见的是:'#!/ bin / bash -ex'。

-e Exit immediately if a command exits with a non-zero status. -x Print commands and their arguments as they are executed.

-e如果命令以非零状态退出,则立即退出。 -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将提供详细输出,并且如果脚本的一部分失败,也会立即中止脚本。