0

This my code

if [[ (grep -x $idle | grep -x $dead | grep -x $busy) || grep -x $idle1 | grep -x $dead | grep -x $busy1 ]] ./Event.log 
then 
    echo "Events are running Successfully" >> ./Event.log 
else 
    echo "One or more Events are down. Check the log and restart the Events." >> ./Event.log
fi

I'm getting the error

0403-057 Syntax error at line 14 : `-x' is not expected.

What's up?

7
  • Are you hoping that grep -x $idle is going to be reading from ./Event.log? What do you want grep -x $idle1 to read from? Commented Aug 23, 2015 at 16:49
  • 2
    Can you explain what you're trying to do? I can't guess; the different pieces of your code don't fit together in any way that makes sense to me. Commented Aug 23, 2015 at 16:49
  • Unless $idle and $dead contain exacty the same string, grep -x $idle | grep -x $dead will never return a match. Commented Aug 23, 2015 at 18:01
  • @tripleee: they could be regexes, in which case it is a reasonable way of finding the conjuction of the two. But you're probably right that it is an error. Commented Aug 23, 2015 at 19:08
  • With -x, the entire input line must match. The output from the code mentions different "events" (in a way which suggests they are actually services or similar) so it seems highly unlikely that you need three different regexes to find one of them. Commented Aug 23, 2015 at 19:12

2 Answers 2

5

In bash, [[ is syntactically a command which is terminated with the matching ]]. It is not part of the syntax of the if command, whose syntax starts:

    if commands ; then

If you want to test whether a command succeeded or not, you just do that:

if grep -q pattern file; then
  # grep found pattern in file
else
  # grep did not find pattern in file
fi

Within a [[ command, bash expects to find a conditional expression, not another command. That's why grep -x ... is a syntax error. -x is a unary operator in a conditional expression, which is true if its argument is the name of an executable file, but in that expression it is being used as though it were a binary operator.

If you wish to test for more than one pattern with grep, you can use the -e option to specify each option; the grep will succeed (or select) lines matching any of the options:

if grep -q -e pattern1 -e pattern2 file; then
  # grep found pattern1 or pattern2 in file
else
  # grep did not find either pattern in file
fi
Sign up to request clarification or add additional context in comments.

Comments

1

By a long shot, I am guessing that you want Event.log to contain one each of either member of the pairs. This could be done with something like

if awk "/^($idle|$idle1)$/ { ++idle; next }
        /^($dead|$dead1)$/ { ++dead; next }
        /^($busy|$busy1)$/ { ++busy; next }
        idle && dead && busy { exit 0 }
        END { exit 1 }' Event.log; then
    echo Yes
else
    echo no
fi

This collects three variables; if all of them are true, the Awk script exits with a success exit code (that's zero); otherwise, it will return failure (any nonzero value).

It would make more sense to print the result from Awk, too, but there is an awful amount of assumptions and guesswork in this answer already.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.