如果多个grep命令的条件不起作用

时间:2014-12-26 12:37:35

标签: bash grep

以下bash片段给出"条件二元运算符预期"

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

出了什么问题?

1 个答案:

答案 0 :(得分:1)

只需更改您的if语句,如下所示

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

OR

您可以在单个grep命令中执行上述操作,如下例所示,因为grep或awk或sed逐行处理输入。

$ 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

在一行中,就像是,

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