bash grep 后 echo 不起作用 returns 没有
bash echo does not work after grep returns nothing
我知道人们通常在 Linux 命令提示符(而不是脚本)中使用 grep。我碰巧将 grep 放在脚本中,并将 运行 放入一个奇怪的案例中。如果 grep 命令 returns 什么都没有,则下一行 echo 不起作用。下面是脚本。
grep "abc" /home/testuser/myapp.log
echo "abc"
这是 grep 的正常行为吗?如果是,为什么?
您可以使用以下命令将 echo
与 grep
:
printf "%s\n" "$(grep -o "abc" /home/testuser/myapp.log)"
参考资料
您的脚本中似乎启用了 set -e
。
通常,如果选择了一行,grep
的退出状态为 0,如果没有选择任何行,则为 1,如果发生错误,则为 2。但是请注意,如果使用 -q 或 --quiet 或 --silent 并选择了一行,即使发生错误,退出状态也是 0。在您的情况下,如果 abc
未在 /home/testuser/myapp.log
中找到,grep
将 return 1.
bash
shell 通常会执行脚本中的下一行,即 echo "abc"
即使 grep
return 的退出状态为 1 . 但是,如果 set -e
启用,bash
将不会在您的脚本中执行更多行。
来自 bash
联机帮助页:
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command
(see SHELL GRAMMAR above), exits with a non-zero status. The shell does not exit if the command that fails is
part of the command list immediately following a while or until keyword, part of the test following the if or
elif reserved words, part of any command executed in a && or || list except the command following the final &&
or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !. If a
compound command other than a subshell returns a non-zero status because a command failed while -e was being
ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits. This option ap‐
plies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT
above), and may cause subshells to exit before executing all the commands in the subshell.
If a compound command or shell function executes in a context where -e is being ignored, none of the commands
executed within the compound command or function body will be affected by the -e setting, even if -e is set and
a command returns a failure status. If a compound command or shell function sets -e while executing in a con‐
text where -e is ignored, that setting will not have any effect until the compound command or the command con‐
taining the function call completes.
“下一行回显不起作用”是什么意思? echo
是不是根本没有达到?它是否 运行 但产生非零退出代码?或者你在输出中看到 abc
了吗?知道你的脚本 return 对 grep
没有任何影响,并且对脚本的其余部分一无所知,我可以说脚本不会在 stdout
上为 [=12= 输出任何内容] 命令和 stdout
上的 abc
用于 echo
。
我知道人们通常在 Linux 命令提示符(而不是脚本)中使用 grep。我碰巧将 grep 放在脚本中,并将 运行 放入一个奇怪的案例中。如果 grep 命令 returns 什么都没有,则下一行 echo 不起作用。下面是脚本。
grep "abc" /home/testuser/myapp.log
echo "abc"
这是 grep 的正常行为吗?如果是,为什么?
您可以使用以下命令将 echo
与 grep
:
printf "%s\n" "$(grep -o "abc" /home/testuser/myapp.log)"
参考资料
您的脚本中似乎启用了 set -e
。
通常,如果选择了一行,grep
的退出状态为 0,如果没有选择任何行,则为 1,如果发生错误,则为 2。但是请注意,如果使用 -q 或 --quiet 或 --silent 并选择了一行,即使发生错误,退出状态也是 0。在您的情况下,如果 abc
未在 /home/testuser/myapp.log
中找到,grep
将 return 1.
bash
shell 通常会执行脚本中的下一行,即 echo "abc"
即使 grep
return 的退出状态为 1 . 但是,如果 set -e
启用,bash
将不会在您的脚本中执行更多行。
来自 bash
联机帮助页:
-e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command
(see SHELL GRAMMAR above), exits with a non-zero status. The shell does not exit if the command that fails is
part of the command list immediately following a while or until keyword, part of the test following the if or
elif reserved words, part of any command executed in a && or || list except the command following the final &&
or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !. If a
compound command other than a subshell returns a non-zero status because a command failed while -e was being
ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits. This option ap‐
plies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT
above), and may cause subshells to exit before executing all the commands in the subshell.
If a compound command or shell function executes in a context where -e is being ignored, none of the commands
executed within the compound command or function body will be affected by the -e setting, even if -e is set and
a command returns a failure status. If a compound command or shell function sets -e while executing in a con‐
text where -e is ignored, that setting will not have any effect until the compound command or the command con‐
taining the function call completes.
“下一行回显不起作用”是什么意思? echo
是不是根本没有达到?它是否 运行 但产生非零退出代码?或者你在输出中看到 abc
了吗?知道你的脚本 return 对 grep
没有任何影响,并且对脚本的其余部分一无所知,我可以说脚本不会在 stdout
上为 [=12= 输出任何内容] 命令和 stdout
上的 abc
用于 echo
。