bash shell 命令行的参数 -e 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9952177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the meaning of the parameter -e for bash shell command line?
提问by Sam Ho
I have as bash shell script with header #!/bin/bash -e
.
我有带有 header 的 bash shell 脚本#!/bin/bash -e
。
When I run the script, it will be interrupted after the grep
command runs, but when I remove the parameter -e
, then the script can be run normally. What is the meaning of parameter -e
?
当我运行脚本时,grep
命令运行后会中断,但是当我删除参数时-e
,脚本可以正常运行。参数的含义是-e
什么?
回答by ruakh
The -e
option means "if any pipeline ever ends with a non-zero ('error') exit status, terminate the script immediately". Since grep
returns an exit status of 1
when it doesn't find any match, it can cause -e
to terminate the script even when there wasn't a real "error".
该-e
选项意味着“如果任何管道以非零('错误')退出状态结束,立即终止脚本”。由于grep
返回1
找不到任何匹配项的退出状态,因此-e
即使没有真正的“错误” ,它也可能导致终止脚本。
If you want to keep the -e
option, but also have a grep
command that might validly find no matches, you can append || :
to the grep
command. This means "or, if the grep
command returns a non-zero exit status, run :
(which does nothing)"; so the net effect is to disable -e
for the grep
command. So:
如果您想保留该-e
选项,但也有一个grep
可能有效地找不到匹配项|| :
的grep
命令,您可以附加到该命令。这意味着“或者,如果grep
命令返回非零退出状态,则运行:
(什么都不做)”;所以净效应是禁用-e
的grep
命令。所以:
grep PATTERN FILE... || :
Edited to add:The above approach discards every error: if grep
returns 1
because it found no matches, that's ignored, but also if grep
returns 2
because there was an error, that's ignored, and if grep
isn't in the path (so Bash returns 127
), that'signored — and so on. So, rather than :
, it's probably better to use a command that checks the result code and re-issues the error if it's something other than 1
. For example:
编辑添加:上述方法丢弃每个错误:如果grep
返回1
是因为没有找到匹配项,则被忽略,但如果grep
返回2
是因为存在错误,则被忽略,如果grep
不在路径中(因此 Bash 返回127
),那就是忽略——等等。因此,:
如果不是1
. 例如:
grep PATTERN FILE || (( $? == 1 ))
But this destroys the exit status; usually, when a failed command terminates a Bash script with -e
, the script will return the command's exit-status, but in the above example, the script will just return 1
. If (and only if) we care about that, we can fix it by write something like this:
但这会破坏退出状态;通常,当失败的命令用 终止 Bash 脚本时-e
,脚本将返回命令的退出状态,但在上面的示例中,脚本只会返回1
. 如果(且仅当)我们关心它,我们可以通过写这样的东西来修复它:
grep PATTERN FILE || exit_code=$?
if (( exit_code > 1 )) ; then
exit $exit_code
fi
(first line c/o dsummersl's comment).
(第一行 c/o dsummersl的评论)。
At this point, it's probably best to create a shell function to handle this for us:
此时,最好创建一个 shell 函数来为我们处理这个问题:
function grep_no_match_ok () {
local exit_code
grep "$@" || exit_code=$?
return $(( exit_code == 1 ? 0 : exit_code ))
}
(note the use of return
rather than exit
; we'll let -e
handle the exiting when appropriate); this way, we can just write:
(注意使用return
而不是exit
;我们会-e
在适当的时候让处理退出);这样,我们可以只写:
grep_no_match_ok PATTERN FILE # won't kill script if no matches are found
In fact, since we most likely want to use this function for alloccurrences of grep
in this script, we can actually just name the function grep
:
实际上,由于我们很可能希望在此脚本中出现的所有情况都使用此函数grep
,因此我们实际上可以将该函数命名为grep
:
function grep () {
local exit_code
command grep "$@" || exit_code=$?
return $(( exit_code == 1 ? 0 : exit_code ))
}
grep PATTERN FILE # won't kill script if no matches are found
(note the use of command
to bypass the shell function within its own body: we want the function to call the regular program grep
, rather than to recurse infinitely).
(注意command
在它自己体内绕过shell函数的用法:我们希望函数调用常规程序grep
,而不是无限递归)。
回答by mu is too short
From the fine manual:
从精美的手册:
In addition to the single-character shell command-line options (see The Set Builtin), there are several multi-character options that you can use.
除了单字符 shell 命令行选项(请参阅 Set Builtin)之外,您还可以使用多个多字符选项。
And then if we look at what set
has to say:
然后,如果我们看看有什么set
要说的:
-e
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.
-e
如果管道(请参阅管道)可能包含单个简单命令(请参阅简单命令)、括在括号中的子 shell 命令(请参阅命令分组)或作为命令列表的一部分执行的命令之一,则立即退出大括号(请参阅命令分组)返回非零状态。
So when you say bash -e
, if any command in the script fails (i.e. returns a non-zero exist status), then the whole script immediately fails. So your grep
is returning a non-zero value because it isn't matching and that's shutting down the whole script if you specify -e
when running bash.
因此,当您说bash -e
,如果脚本中的任何命令失败(即返回非零存在状态),则整个脚本立即失败。因此,您grep
将返回一个非零值,因为它不匹配,并且如果您-e
在运行 bash 时指定,则会关闭整个脚本。