Bash shell,while 循环中的变量无法保存问题
来源:互联网 发布:linq to sql 编辑:程序博客网 时间:2024/06/01 09:39
问题SHELL举例:
#!/bin/sh
Counter=0
ls /opt/src/ | while read LINE
do
let "Counter=Counter+1"
done
echo $Counter
该SHELL用于计算/opt/src/中有多少文件,但实际运行中无论/opt/src/下有多少文件,最后输出都是0。
经查,造成这个问题的原因是while循环位于管道中,这意味着在运行过程中,while循环实际是位于一个新的SHELL中的,while循环中的Counter变量和文件开头定义的Counter变量是两个不同的变量,所以while循环中所改变的值在while循环结束后无法保存下来。
解决办法:
#!/bin/sh
Counter=0
ls /opt/src/ > /tmp/t1.txt
while read LINE
do
let "Counter=Counter+1"
done < t1.txt
rm /tmp/t1.txt -f
echo $Counter
- Bash shell,while 循环中的变量无法保存问题
- Bash中的While循环
- Shell中的while循环
- Shell中的while循环
- SHELL中的while循环
- shell while循环问题
- Shell变量while循环内改变无法传递到循环外
- shell 中的for循环和while循环
- Linux命令之while - Bash中的While循环
- Linux命令之while - Bash中的While循环
- 关于while read line循环的linux bash shell bug
- Shell中的循环语句for、while、until
- bash shell while语法
- bash shell while语法
- shell 循环变量传递问题
- shell 中的for循环while循环和case语句
- Bash Shell 的 嵌套的While带来的问题
- shell 预定义变量, for while循环 ,函数
- squid缓存系统全攻略
- hadoop context.write() 以多个字段为key
- 数据库之事务
- Dreamweaver代码提示快捷键:Ctrl+H
- linux下解决编译.bashrc后命令丢失问题
- Bash shell,while 循环中的变量无法保存问题
- 如何调试Javascript代码
- 录音
- Android初试--资源目录下的文件详解
- Maven学习笔记(一)
- linux中getopt的用法
- startService与bindService
- windows下sc create命令行添加/创建/修改服务
- softmax 回归