bash 执行由前一个输出确定的命令(即,仅当有一些输出时)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5524530/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:43:53  来源:igfitidea点击:

Execute command determined by output of previous one (i.e., only if there was some output)

bashscriptingcommand-line

提问by slhck

Should be fairly simple to answer:

回答应该很简单:

Let's say I wanted to execute a command determined by the output of a previous one in Bash:

假设我想执行一个由 Bash 中前一个命令的输出确定的命令:

curl http://website.com 2> /dev/null | grep -i "test" --count | <MY-COMMAND>

What I need:<MY-COMMAND>should only execute if grephad some matches (at least 1).

我需要的是:<MY-COMMAND>只有在grep有一些匹配项(至少 1 个)时才应该执行。

How can I achieve that?

我怎样才能做到这一点?

Also, please feel free to add matching tags, I couldn't come up with any

另外,请随意添加匹配的标签,我想不出任何

回答by imz -- Ivan Zakharyaschev

ifneutility ("run a program if the standard input is not empty") from Jeoy Hess's moreutilspackage will serve you.

ifne来自 Jeoy Hess 的moreutils包的实用程序(“如果标准输入不为空,则运行程序”)将为您服务。

A description of it:

对其的描述:

a command that would run the following command if and only if the standard input is not empty. I often want this in crontabs, as in:

find . -name core | ifne mail -s "Core files found" root

当且仅当标准输入不为空时,才会运行以下命令的命令。我经常希望在 crontabs 中使用它,例如:

find . -name core | ifne mail -s "Core files found" root

回答by John Kugelman

Do you need the output of grep to be piped to your command? The answer is simpler if you do not. In that case since grep's return code is success only if it finds a match, you can use &&or if:

您是否需要将 grep 的输出通过管道传送到您的命令?如果你不这样做,答案会更简单。在这种情况下,由于 grep 的返回码仅在找到匹配项时才成功,您可以使用&&if

curl http://website.com 2> /dev/null | grep -q -i "test" && <MY-COMMAND>

if curl http://website.com 2> /dev/null | grep -q -i "test"; then
    <MY-COMMAND>
fi

The &&operator is a shorthand way of performing an if-else check. It is a short-circuiting operator, which means that the right hand side will only be executed if the left hand side fails.

&&运营商执行的if-else检查的简便方法。它是一个短路运算符,这意味着只有在左侧失败时才会执行右侧。

If you need to pipe the output to your command then you'll need to save the output to a temporary file, test for a match, and then execute your command:

如果您需要将输出通过管道传输到您的命令,那么您需要将输出保存到一个临时文件中,测试是否匹配,然后执行您的命令:

if curl http://website.com 2> /dev/null | grep -i "test" > /tmp/grep.txt; then
    <MY-COMMAND> < /tmp/grep.txt
fi

回答by Ulrik

curl http://website.com 2> /dev/null | grep -i "test" && <MY-COMMAND>

From the grep man page: "the exit status is 0 if selected lines are found and 1 otherwise"

从 grep 手册页:“如果找到选定的行,退出状态为 0,否则为 1”

The command after && is executed only if the previous command returned exit status 0.

&& 之后的命令仅在前一个命令返回退出状态 0 时才执行。

回答by Ankur Agarwal

curl http://www.google.com 2>/dev/null | grep window -i -c && echo "this is a success"