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跟循环是平行的; 

 

。 

 

posted @   小鲨鱼2018  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2022-04-01 samtools安装报错 configure: error: curses development files not found
2022-04-01 centos中安装 gcc编译器
2022-04-01 linux中批量测试ip是否在线
2022-04-01 linux 系统中批量创建用户
2022-04-01 linux 中if条件判断语句
2022-04-01 linux中创建用户、设置密码、彻底删除用户
2022-04-01 linux 中如何查看当前创建的所有用户
点击右上角即可分享
微信分享提示