Hi everyone
i am facing a strange problem in grep below is the code
RC=0
grep $ERROR_MASK $LOG_FILE 2>&1 > /dev/null && RC=1 || RC=0
what does the above statment do i mean it search for error mask into log file and redirect the error to console null then what's the meaning of RC=1 || RC=0
please help me regarding this
If grep command is successful, it will set value of RC to 1, else it will set it to 0
That statement contains a mistake and is a bit cumbersome.
The author probably meant to do this:
RC=0
grep "$ERROR_MASK" "$LOG_FILE" > /dev/null 2>&1 && RC=1 || RC=0
Which kills all output. However RC=0 is superfluous, as this will produce the same result:
grep "$ERROR_MASK" "$LOG_FILE" > /dev/null 2>&1 && RC=1 || RC=0
However, this can also be achieved by this:
! grep "$ERROR_MASK" "$LOG_FILE" > /dev/null 2>&1
RC=$?
or
! grep -q "$ERROR_MASK" "$LOG_FILE"
RC=$?
What it does is putting a return code into variable RC (1 is wrong, 0 is succesful)..
In short: if the log file does not contain the error_mask then the outcome is good (RC=0)