Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

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

How can I achieve that?

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

like image 401
slhck Avatar asked Dec 12 '22 14:12

slhck


1 Answers

ifne utility ("run a program if the standard input is not empty") from Jeoy Hess's moreutils package will serve you.

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
like image 177
imz -- Ivan Zakharyaschev Avatar answered Dec 29 '22 00:12

imz -- Ivan Zakharyaschev