I was trying a simple program to compare the string values stored on a log file and was getting an error as below,
#!/bin/bash
check_val1="successful"
check_val2="completed"
log="/compile.log"
if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]];
then echo "No Error"
else echo "Error"
fi
Error:
./simple.sh: line 7: conditional binary operator expected
./simple.sh: line 7: syntax error near `$check_val1'
./simple.sh: line 7: `if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]];' 2 Answers
Problem is in your if [[...]] expression where you are using 2 grep commands without using command substitution i.e. $(grep 'pattern' file).
However instead of:
if [[ grep $check_val1 $log -ne $check_val1 || grep $check_val2 $log -ne $check_val2 ]]; thenYou can use grep -q:
if grep -q -e "$check_val1" -e "$check_val2" "$log"; thenAs per man grep:
-q, --quiet, --silent Quiet mode: suppress normal output. grep will only search a file until a match has been found, making searches potentially less expensive. 11 [[ trigers the test command. Test doesn't support testing the exit status of a command just by typing the command