2

I'm running Puppet, and I need to run one of the defined exec commands only if a line does not exist in the output from a different command. I can set up a contingency with a unless clause (which will run the command unless another command returns exit code 0) or with a onlyif clause (which will run a command only if another command returns exit code 0).

Is there a standard Linux command which will verify a particular substring exists in the input, and return an exit code otherwise?

1 Answer 1

3

Would grep do what you want ?

In particular

grep -q something somefile

will exit with status 0 if something was in somefile and 1 if not.

The somefile can be a pipe too

somecommand | grep -q something

will exit with status 0 if the output from somecommand contains something.

3
  • 2
    The standard shell idiom is to if somecommand | grep -q expected ; then it-was-there ; else scream-it-was-not-there ; fi
    – vonbrand
    Feb 20, 2013 at 20:22
  • @Matt: grep -q conforms to POSIX. (A little late, but just in case someone else stumbles across this.)
    – Blrfl
    Mar 12, 2015 at 11:52
  • So it does.. I'm struggling to remember where I picked up that impression now. I think it was Solaris 9's standard grep. Everything else seems to support it. Thanks! Erroneous comment baleeted.
    – Matt
    Mar 14, 2015 at 13:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .