nikel
nikel

Reputation: 3564

if condition with multiple grep commands not working

The following bash snippet gives "conditional binary operator expected"

countries=$1
while read -r line; do
        if [[ echo "$line" | grep BUSINESS | grep -E "$countries" ]]; then
                echo $line >> "$Business_Accounts"
        fi
done

What's going wrong?

Upvotes: 2

Views: 6450

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Just change your if statement like below,

if [[ $(echo "$line" | grep BUSINESS | grep -E "$countries") ]]; then

OR

You could do like the above in a single grep command like in the below example because grep or awk or sed processes the input line by line.

$ contries="foo"
$ echo 'foo BUSINESS bar
bar' | grep -P "^(?=.*BUSINESS).*$con"
foo BUSINESS bar
$ Business_account=$(echo 'foo BUSINESS bar                                
bar' | grep -P "^(?=.*BUSINESS).*$con")
$ echo "$Business_account"
foo BUSINESS bar

In a single line, it would be like,

Business_account=$(grep -P "^(?=.*BUSINESS).*$contries" file)

Upvotes: 3

Related Questions