带有grep的if语句的bash语法错误

时间:2015-04-06 14:37:31

标签: linux bash

i等于0时,我在第7行收到语法错误(并且if应该失败)。如果我在grep成功且i不为0的文件上使用它,则程序可以正常工作。

可能是什么问题?

#!/bin/bash

function search_author_tests {
    while read line; do
        i=`grep -c "commit version: "$line"" "$1"/QA/*.tst`
        echo $i
        if (($i>0)); then
            echo -n $line 
            grep "result" `grep -wl "commit version: "$line"" "$1"/QA/*.tst`|cut -d ":" -f2
        fi  
    done
}   


grep -w "$2" "$1"/*.comp | cut -d" " -f2 | sort -n| search_author_tests $1

示例错误:  第7行:((:/ u1 / 044 / daramram / mtm / ex1 / q3 / test3 / cv / QA / 1.tst:1 /u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/2.tst:0>0:语法错误:操作数预期(错误标记为" / u1 / 044 / adaramir / mtm / ex1 /q3/test3/cv/QA/1.tst:1 /u1/044/adaramir/mtm/ex1/q3/test3/cv/QA/2.tst:0>0")

2 个答案:

答案 0 :(得分:0)

您正在从多个文件中搜索,并且您会看到每个文件的计数。 将第7行的作业更改为

i=$(cat "$1"/QA/*.tst 2>/dev/null | grep -c "commit version: $line")

答案 1 :(得分:0)

当“grep -c”与多个文件一起使用时,其输出格式为:

<file1>:<count1>
<file2>:<count2>

的解决方案: 1.指定单个文件而不是* .txt

  1. 变化

    #!/斌/庆典

    function search_author_tests {
        while read line; do
            i=`grep -c "commit version: "$line"" "$1"/QA/*.tst | cut -d: -f2 | sort -nr | head -1`
            echo $i
            if (($i>0)); then
                echo -n $line 
                grep "result" `grep -wl "commit version: "$line"" "$1"/QA/*.tst`|cut -d ":" -f2
            fi  
        done
    }   
    
    
    grep -w "$2" "$1"/*.comp | cut -d" " -f2 | sort -n| search_author_tests $1
    
  2. 由于