Take the 2-minute tour ×
Server Fault is a question and answer site for professional 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
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 '13 at 20:22
    
I know the question is tagged linux but wanted to note -q is gnu, grep expected >/dev/null if you want cross platformness. –  mtm Feb 20 '13 at 21:31

Your Answer

 
discard

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.