一、问题

问题很简单,看下面一段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. VS Code 折腾记 - (6) 基本配置/快捷键定义/代码片段的录入(snippet)

    前言 本来分成三篇来写的,但是想了想没必要,大家都是聪明人...简单的东西点一下就晓得了. 基本配置 快捷键自定义(Ctrl+K Ctrl + S) 那个when支持条件表达式返回一个布尔值 支持的快 ...

  2. 云平台Linux主机安装流程

    ==一.安装包===================================================================================如果是1+2主机安装 ...

  3. [转] Java基础知识——Java语言基础

    http://blog.csdn.net/loneswordman/article/details/9905931 http://blog.csdn.net/wanghuan203/article/d ...

  4. Codeforces 830C Bamboo Partition (看题解)

    Bamboo Partition 列公式, 整除分块, 想不到, 好菜啊. #include<bits/stdc++.h> #define LL long long #define fi ...

  5. Mysql数据库改名

    转自:https://www.cnblogs.com/leffss/p/7832100.html #!/bin/bash #作者:fafu_li #时间:2015.08.10 #mysql数据库改名, ...

  6. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  7. BZOJ4974 八月月赛 Problem D 字符串大师 KMP

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4974 - 八月月赛 Problem D 题意概括 一个串T是S的循环节,当且仅当存在正整数k,使得 ...

  8. 043 HIVE中的HQL操作

    1.字段查询 select empno,ename from emp; 2.过滤where,limit,distinct select * from emp where sal >2500; s ...

  9. [OpenCV-Python] OpenCV 中的图像处理 部分 IV (三)

    部分 IVOpenCV 中的图像处理 OpenCV-Python 中文教程(搬运)目录 19 Canny 边缘检测 目标 • 了解 Canny 边缘检测的概念 • 学习函数 cv2.Canny() 1 ...

  10. Linux学习之分区自动挂载与fstab文件修复(九)

    linux分区自动挂载与fstab文件修复 在前面我们实现新添加硬盘,进行分区与格式化,然后手动挂载,这样做,在重启后,需要重新挂载才能使用. https://www.cnblogs.com/-wen ...