Bash - while循环文件和停止条件

时间:2014-02-27 09:47:48

标签: bash while-loop

我想从/ proc文件中读取一个特定的值,目前这是在'while read line'循环中完成的,作为一个方案:

while read line
do
    if [[ $line = *MATCHES STRING* ]]; then
        for ((  i=0 ;  i<=$someVAR-1;  i++ ))
        do
          [if statement to check if the specific line is not equal with 0]
          then [whatever]
        done
    fi
done < "/proc/FILE"

有了这个,我想进一步改进循环,这样我就可以计算for循环中if语句返回0以上值的次数。 所以停止条件应该是:

if line matches string then
for loop
do
use a variable to keep count that the conditions has been met 2 times.

当发生这种情况时,循环应该停止并显示一条消息。

1 个答案:

答案 0 :(得分:1)

while read line
do
    if [[ $line = *MATCHES STRING* ]]; then
        count=0
        for ((  i=0 ;  i<=$someVAR-1;  i++ ))
        do
            [if statement to check if the specific line is not equal with 0]
            then count=$((count+1))
            fi
        done
        if [[ $count -ge 2 ]]
        then break
        fi
    fi
done < "/proc/FILE"