首页 > 代码库 > shell while内获取外部变量内容
shell while内获取外部变量内容
一、问题
问题很简单,看下面一段tmp.sh代码:
#!/bin/shx="this is the initial value of x"cat /tmp/tmp | while read line;do x="$line" echo $xdoneecho x = $x
/tmp/tmp的内容
1,a
执行 ./tmp.sh,正常x变量是蓝色的“1,a”,但是实际的结果却是红色部分:
[qiu.li@l-tdata1.tkt.cn6 ~]$ ./tmp.sh 1,ax = this is the initial value of x
问题很明显,变量x在while内修改之后,并没有带到外面来,为什么哪?
二、答案
经过几经查找,终于发现问题的原因:是因为启动了子进程,而变量x在子进程修改之后,并没有带入到父进程。原文如下:
This is because in the Bourne shell redirected control structures run in a subshell, so the value of x only gets changed in the subshell, and is lost when the loop ends.In other shells the same result may be seen because of the way pipelines are handled. In shells other than ksh (not pdksh) and zsh elements of a pipeline are run in subshells. In ksh and zsh, the last element of the pipeline is run in the current shell.An alternative for non-Bourne shells is to use redirectioninstead of the pipeline
如何解决哪?
1、一般方法:
#!/bin/shx="this is the initial value of x"while read line;do x="$line" echo $xdone < /tmp/tmpecho x = $x
运行:
[qiu.li@l-tdata1.tkt.cn6 ~]$ ./tmp.sh 1,ax = 1,a
2、优雅一些的解决方法:
#!/bin/shx="this is the initial value of x"exec 3<&0 # save stdinexec < /tmp/tmpwhile read line; do x=$line echo $xdoneexec 0<&3 # restore stdinecho x = $x
本人英文实在有限,原因只能贴出英文:
Note that putting #!/bin/sh at the top of a script doesn‘t guarantee you‘re using the Bourne shell. Some systems link /bin/sh to some other shell. Check your system documentation to find out what shell you‘re really getting in this case.
大概是说: 注意最上面的#!/bin/sh 如果使用其他的此脚本不能运行。但是在有些系统/bin/sh 被指定到了其他的shell解释器里面,也是出现不一样的结果。详细的参考:http://www.cnblogs.com/liqiu/p/4107948.html
shell while内获取外部变量内容
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。