Conditional binary operator expected in shell script

I tried a simple program to compare string values ​​stored in a log file, and received an error as shown 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 ]];' 
+8
shell
source share
2 answers

The problem is the if [[...]] expression, where you use 2 grep commands without using the command substitution ie $(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 

According to 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. 
+13
source share

[[ runs the test command. The test does not support testing the exit status of a command, simply by typing a command

+1
source share

All Articles