- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道人们通常在 Linux 命令提示符(而不是脚本)中使用 grep。我碰巧将 grep 放在脚本中并遇到了一个奇怪的情况。如果 grep 命令没有返回任何内容,则下一行 echo 不起作用。下面是脚本。
grep "abc" /home/testuser/myapp.log
echo "abc"
这是 grep 的正常行为吗?如果是,为什么?
最佳答案
您的脚本中似乎启用了 set -e
。
通常,grep
的退出状态如果选择了一行则为 0,如果没有选择任何行则为 1,如果发生错误则为 2。但是请注意,如果使用 -q 或 --quiet 或 --silent 并选择了一行,即使发生错误,退出状态也是 0。在您的情况下,如果在 /home/testuser/myapp.log
中找不到 abc
,grep
将返回 1。
bash
shell 通常会执行脚本中的下一行,即 echo "abc"
即使 grep
返回退出状态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.
关于linux - bash echo 在 grep 什么都不返回后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70468619/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!