for 和 while循环方式的差异

 

001、

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt                                          ## 测试数据
1
2
3
[root@pc1 test]# sum=0
[root@pc1 test]# for i in $(seq 3); do let sum+=$i; done             ## for循环,输出变量
[root@pc1 test]# echo $sum
6
[root@pc1 test]# sum=0
[root@pc1 test]# cat a.txt | while read i; do let sum+=$i; done      ## while循环不能输出变量???
[root@pc1 test]# echo $sum
0

。 

 

posted @ 2024-04-01 12:46  小鲨鱼2018  阅读(17)  评论(0)    收藏  举报