Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
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 '13 at 20:22
@Matt: grep -q conforms to POSIX. (A little late, but just in case someone else stumbles across this.) – Blrfl Mar 12 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 at 13:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.