shell 循环变量传递问题
2016-04-07 17:00
351 查看
如例子中:
执行结果如下(后面一部分)
为什么变量num没有被传递?
定义为环境变量没有用的,环境变量只是在子进程创建的时候可以从父进程复制到子进程,它无法实现从子进程往父进程传递,也不能在子进程运行期间从父进程获得新值。
解决办法是不要产生子进程
如下:
执行结果:
#!/bin/bash file="/etc/passwd" let num=0 cat $file | while read line do echo -e "hello,`echo $line|cut -d ":" -f 1` \c" echo your UID is `echo $line|cut -d ":" -f 3` num=$[$num+1] echo $num done echo number is $num
执行结果如下(后面一部分)
hello,hplip your UID is 113 32 hello,saned your UID is 114 33 hello,lsn your UID is 1000 34 hello,sshd your UID is 115 35 number is 0
为什么变量num没有被传递?
定义为环境变量没有用的,环境变量只是在子进程创建的时候可以从父进程复制到子进程,它无法实现从子进程往父进程传递,也不能在子进程运行期间从父进程获得新值。
解决办法是不要产生子进程
如下:
#!/bin/bash file="/etc/passwd" let num=0 while read line do echo -e "hello,`echo $line|cut -d ":" -f 1` \c" echo your UID is `echo $line|cut -d ":" -f 3` num=$[$num+1] echo $num done < $file echo number is $num
执行结果:
hello,speech-dispatcher your UID is 112 31 hello,hplip your UID is 113 32 hello,saned your UID is 114 33 hello,lsn your UID is 1000 34 hello,sshd your UID is 115 35 number is 35
相关文章推荐
- 如何使用PowerShell实时获取自己的公网IP地址 推荐
- bash配置文件/etc/profile,/etc/bashrc,~/.bash_profile,~/.bashrc详解及修改PATH环境变量
- Linux IP代理筛选系统(shell+proxy)
- Linux IP代理筛选系统(shell+proxy)
- shell_sctipts: 删除mysql备份到最后7日
- shell常用命令总结(转)
- 使用 git commit -m 命令写多行注释
- win7下, 修改git bash的默认开始路径
- linux关于bashrc与profile的区别(转)
- xshell中Ctrl+S锁定屏幕
- LINUX下使用Shell自动监控tomcat并且执行重启操作 --谷营中西软件科技园
- Xshell中文乱码怎么处理?
- scp -r authorized_keys zdb14:~/.ssh/ root@zdb14's password: bash: scp: command not found lost conn
- shell编程之环境配置文件
- 如何进行shell脚本正确性测试
- 终极 Shell
- linux shell重定向总结
- HBase 基本shell命令
- Windows PowerShell 管理 Office 365
- 修改gnome-shell扩展“Applications Menu”的菜单区域宽度。