0

well i just dont know what happened, my grep results counter used to work and now it seems that no matter what i do it doesn't count my results and stay on the initial value of 0, at the first line of the script i initiating it:

TotalResults=0

even if i define it in that way:

typeset -i TotalResults=0

it won't work , that's the while loop which in it the counter should grow and it actually doing the other commands, it's doing the printf stuff but just not increasing the counter, i checked it with echo and also when i want to use it, it stays on 0!

export URL="$CurrentURL"

grep -n -o -a $ExpressionValue $INDEX | while read line ; do

      printf "%s\t%s" "${URL} ${line}"
      printf "\n"
      let TotalResults+=1

done

what is the problem? I have other counter that defined the same and he is working great, I'm tired of that, please help.

4

2 回答 2

2

在 | 之后,您正在增加子外壳中的计数器。变量在父 shell 中不会改变。将您的代码更改为

while read line ; do

      printf "%s\t%s" "${URL} ${line}"
      printf "\n"
      let TotalResults+=1

done < <(grep -n -o -a $ExpressionValue $INDEX)
于 2011-11-25T10:46:49.927 回答
0

我建议使用 c 风格的计数器,因为代码变得更具可读性并且运行速度更快:

while read line ; do

      printf "%s\t%s" "${URL} ${line}"
      printf "\n"
      (( ++TotalResults))

done < <(grep -n -o -a $ExpressionValue $INDEX)
于 2011-11-26T08:27:03.730 回答