“#!/bin/sh -xe” 是什么意思?

6
Tribler的GitHub存储库中,我看到一个带有解释器#!/bin/sh -xe文件

在Mac和Linux上,man sh重定向到BASH(1)(与man bash相同)。
在这个手册文件中,在章节下面:

OPTIONS

中没有提到选项-x或选项-e
解释器#!/bin/sh -xe的含义是什么?

1
对于 -e,可以参考这个答案 - alegria
1
OPTIONS 部分的第一行说明了在启动 bash 时,您可以使用所有在 set 内置文档中记录的选项(包括 -x-e)。接下来的标志只是额外的标志。 - chepner
2个回答

9

I found this:

man sh:

-x xtrace        Write each command to standard error (preceded by
                            a ‘+ ’) before it is executed.  Useful for debug‐
                            ging.


-e errexit       If not interactive, exit immediately if any
                            untested command fails.  The exit status of a com‐
                            mand is considered to be explicitly tested if the
                            command is used to control an if, elif, while, or
                            until; or if the command is the left hand operand
                            of an “&&” or “||” operator.

看起来这些只是错误控制选项,如果一个命令失败了,它们确保不会发生更糟糕的情况。


请注意,使用-e可能会非常适得其反,会在本来没有错误的情况下产生错误。请参阅BashFAQ#105中的练习以及不同shell如何处理此标志的不兼容性列表,网址为https://www.in-ulm.de/~mascheck/various/set-e/。 - Charles Duffy

1

按照man手册: 阅读更多

-a,-b,-C,-e,-f,-m,-n,-o选项,-u,-v和-x选项被描述为特殊内置实用程序中的一部分。从set特殊内置派生的选项字母也可以接受一个前导加号('+')而不是一个前导连字符(表示本卷IEEE Std 1003.1-2001中所述选项的反向情况)。

当您阅读set man页面时,您会得到:

-x shell在扩展命令并在执行命令之前将跟踪写入标准错误。未指定是否跟踪关闭跟踪的命令。

以及

-e 当此选项打开时,如果一个简单的命令因Shell错误或返回一个大于0的退出状态值而失败,并且不是while、until或if关键字后面的复合列表的一部分,也不是AND或OR列表的一部分,并且不是由!保留字前导的管道,则Shell将立即退出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接