shell while内获取外部变量内容

一、问题

问题很简单,看下面一段tmp.sh代码:

#!/bin/sh
x="this is the initial value of x"
cat /tmp/tmp | while read line;do
        x="$line"
        echo $x
done
echo x = $x

/tmp/tmp的内容

1,a

执行 ./tmp.sh,正常x变量是蓝色的“1,a”,但是实际的结果却是红色部分:

[qiu.li@l-tdata1.tkt.cn6 ~]$ ./tmp.sh 
1,a
x = 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 redirection
instead of the pipeline

如何解决哪?

1、一般方法:

#!/bin/sh
x="this is the initial value of x"
while read line;do
        x="$line"
        echo $x
done < /tmp/tmp
echo x = $x

运行:

[qiu.li@l-tdata1.tkt.cn6 ~]$ ./tmp.sh 
1,a
x = 1,a

2、优雅一些的解决方法:

#!/bin/sh
x="this is the initial value of x"
exec 3<&0         # save stdin 将标准输入重定向到文件描述符3
exec < /tmp/tmp # 输入文件
while read line; do
        x=$line
        echo $x
done
exec 0<&3        # restore stdin
echo x = $x

上面使用的方法是:使用重定向代替子shell,从而可以在外面获取while里面的变量值。

参考:http://www.cnblogs.com/liqiu/p/4107948.html

转载于:https://www.cnblogs.com/liqiu/p/4128759.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值