Conditional Binary Operator Expected in Shell Script
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"...
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 ]]; then
You can use grep -q:
if grep -q -e "$check_val1" -e "$check_val2" "$log"; then
As 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.
[[ trigers the test command. Test doesn't support testing the exit status of a command just by typing the command