一、问题

问题很简单,看下面一段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 <& # save stdin 将标准输入重定向到文件描述符3
exec < /tmp/tmp # 输入文件
while read line; do
x=$line
echo $x
done
exec <& # restore stdin
echo x = $x

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

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

shell while内获取外部变量内容的更多相关文章

  1. PHP 闭包获取外部变量和global关键字声明变量的区别

    最近在学习workerman的时候比较频繁的接触到回调函数,使用中经常会因为worker的使用方式不同,会用这两种不同的方式去调用外部的worker变量,这里就整理一下PHP闭包获取外部变量和glob ...

  2. awk获取外部变量

    语法 awk [ -F re] [parameter...] ['pattern {action}' ] [-f progfile][in_file...] 获得普通外部变量 [xingxing.dx ...

  3. 【shell】awk引用外部变量

    在使用awk的过程中,经常会需要引用外部变量,但是awk需要使用单引号将print包起来,导致print后的$引用无效,可以采用下面的方式 例如: #!/bin/bash a="line1 ...

  4. awk内置函数、外部变量

    外部变量 ①获取外部变量 格式: awk '{action}' 变量名=变量值 ,这样传入变量可以在action中获得值. 示例: test='awk test'--day-5 外部变量 ①获取外部变 ...

  5. FileReader对象异步获取外部文件的内容

    1.在网页表单中,定义input的type为file,就可以打开存储在计算机上的文件. <!DOCTYPE html> <head> <meta charset=&quo ...

  6. python 获取系统环境变量 os.environ and os.putenv

    从一段code说起 “if "BATCH_CONFIG_INI" in os.environ:” 判断环境变量的值有没有定义 如果定义的话就去环境变量的值,否则就取当前目录下的co ...

  7. shell的内建命令和外部命令

    shell的内建命令和外部命令 Shell执行的命令可以分为内建命令(built-in)和外部命令(external),前者是构建在shell内部:后者是一个独立的文件(可以是二进制文件,也可以是一个 ...

  8. Bean Shell常用内置变量总结

    JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:写入信息到jmeber.log文件,使用方法:log.info( ...

  9. (未完成)在block内如何修改block外部变量

    变量必须用__block修饰,否则编译不通过 block内部会把变量拷贝到堆区 变量从栈区copy->堆区 通过对对象取地址,打印出对象在内存中的地址 &a block不允许修改外部变量 ...

随机推荐

  1. 搬家通知博文地址(将博客搬到CSDN)

    (为了确认是您本人在申请搬家,请在原博客发表一 篇标题为<将博客搬至CSDN>的文章,并将文章地址填写在上方的"搬家通知博文地址"中.)

  2. (第7篇)灵活易用易维护的hadoop数据仓库工具——Hive

    摘要: Hive灵活易用且易于维护,十分适合数据仓库的统计分析,什么样的结构让它具备这些特性?我们如何才能灵活操作hive呢? 博主福利 给大家推荐一套hadoop视频课程 [百度hadoop核心架构 ...

  3. Redis数据结构之字符串

    学习阶段分成两个部分,一个是redis客户端,一个是java客户端操作 一:在redis客户端操作 1.先删除里面的几个key 2.set与get与getset 3.数值的增减 值递增1,或者减一 如 ...

  4. UVa 11059 - Maximum Product 最大乘积【暴力】

    题目链接:https://vjudge.net/contest/210334#problem/B 题目大意:Given a sequence of integers S = {S1, S2, . . ...

  5. IntelliJ IDEA关于logger的live template配置

    1.安装 log support2插件 2.配置log support2 由于项目中的日志框架是公司自己封装的,所以还需要自己手动改一下 log support2插件生成的live template ...

  6. Leaf:美团分布式ID生成服务开源

    Leaf是美团基础研发平台推出的一个分布式ID生成服务,名字取自德国哲学家.数学家莱布尼茨的一句话:“There are no two identical leaves in the world.”L ...

  7. C# DataGridView搜索

    public static bool SearchDGV(DataGridView DGV, string strTxt, bool UpSearch = true, bool Show = fals ...

  8. [ 转载 ]Java:成员变量,局部变量,静态变量的区别

    精简后方便自己理解. 成员变量 我们研究一个事物: 属性:外在特征:如身高,体重 行为:能做什么:如说话,打球. 在Java语言中,最基本的单位是类(class),类就是用来体现事物的. 属性:类中的 ...

  9. Angularjs 根据数据结构创建动态菜单无限嵌套循环--指令版

    目标:根据数据生成动态菜单,希望可以根据判断是否有子集无限循环下去. 菜单希望的样子是这样的: 菜单数据是这样的: $scope.expanders = [{ title: 'title1', lin ...

  10. AGC027 C - ABland Yard 拓扑排序

    目录 题目链接 题解 代码 题目链接 AGC027 C - ABland Yard 题解 发现有解的充要条件是有一个形为AABBAABBAABB的环 此时每一个点至少与两个不同颜色的点相连 对于初始不 ...