程序1
#!/bin/sh
count=0
cat temp.txt|while read fre
do
count=$(($count+$fre))
done
echo "$count"
exit 1
程序2
#!/bin/sh
count=0
exec 3< temp.txt
while read fre <&3
do
count=$(($count+$fre))
done
echo "$count"
exit 1
程序3
#!/bin/sh
count=0
while read fre
do
count=$(($count+$fre))
done<temp.txt
echo "$count"
exit 1
temp.txt文件
2
2
在运行以上3个程序以后我们会发现
1程序输出结果是0
2,3的输出结果为正确的4
原因是因为程序1使用了管道,管道中的while是在子shell中运行的,并不能返回到父shell中
所以count的值成为了初始化时的值
最后
以上就是俊逸手链最近收集整理的关于shell 关于 while 循环中赋值的问题的全部内容,更多相关shell内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复