0

I am trying to write a shell script which will grep for keywords which should be excluded from a file, if it finds anything else from excluded one, It should execute specific command..

logcheck=`grep -vq 'accepting\|idle.' /tmp/lpstat-Final.log`

    if [[ $logcheck == 'accepting\|idle.' ]] ; then
        status=0
        statustxt=OK
    else
    echo "Something found"
    fi

The outcome should be If anything apart from 'accepting\|idle.' found it should go to else statement or else the exist status will be 0

1
if ! grep -Evq 'accepting|idle' /tmp/lpstat-Final.log; then
    status=0
    statustxt=OK
else
    echo 'Something found'
fi

The main thing here is that grep will not produce any output if you use -q. This means that using a command substitution will not capture anything ($logcheck in your code will be empty). Instead, we use the exit status of the utility to determine whether something was found or not.

I'm using -E here to make the regular expression prettier; it is also needed for alternation (alteration with \| in a basic regular expression is an extension, supported by e.g. GNU grep).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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