linux 中 while两种循环的差异
001、
[root@pc1 test]# ls a.txt [root@pc1 test]# sum=0 [root@pc1 test]# cat a.txt | while read i; do let sum+=$i; done ## 循环方式1 [root@pc1 test]# echo $sum 0 [root@pc1 test]# sum=0 [root@pc1 test]# while read i; do let sum+=$i; done < a.txt ## 循环方式2 [root@pc1 test]# echo $sum 6
猜测:
出现这个原因说明cat a.txt | while read i;这种循环sum变量是局部的;
前者的cat a.txt在循环内部;
后者 < a.txt跟循环是平行的;

。

浙公网安备 33010602011771号