免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2922 | 回复: 7
打印 上一主题 下一主题

echo "xxxxx" | read VAR 为什么var不能取到值 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-05-09 10:38 |只看该作者 |倒序浏览
read 不是从stdin读吗

论坛徽章:
0
2 [报告]
发表于 2008-05-09 10:41 |只看该作者
在子进程里可以得到VAR
echo "xxxxx" | { read VAR;echo $VAR;}

[ 本帖最后由 li2002 于 2008-5-9 10:43 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2008-05-09 12:00 |只看该作者
read是bash内部命令,需要用子shell打开一个另一个进程:

echo "xxx" |(read VAR && echo $VAR)

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
4 [报告]
发表于 2008-05-09 12:19 |只看该作者
在当前shell可以这样
read var <  <(echo xxx);echo $var

论坛徽章:
0
5 [报告]
发表于 2008-05-09 13:28 |只看该作者
有空看看FAQ http://bbs.chinaunix.net/thread-551026-2-1.html


  1. 33. Why do I lose the value of global variables that are set in a loop.

  2.     Given the following program

  3.       #!/bin/sh
  4.       x="this is the initial value of x"
  5.       cat dataFile | while read line;do
  6.         x="$line"
  7.       done
  8.       echo x = $x

  9.      You may get the following for output

  10.        x = this is the initial value of x
  11.    
  12.      This is because in the Bourne shell redirected control structures
  13.      run in a subshell, so the value of x only gets changed in the
  14.      subshell, and is lost when the loop ends.

  15.      In other shells the same result may be seen because of the way
  16.      pipelines are handled. In shells other than ksh (not pdksh) and
  17.      zsh elements of a pipeline are run in subshells. In ksh and zsh,
  18.      the last element of the pipeline is run in the current shell.

  19.      An alternative for non-Bourne shells is to use redirection
  20.      instead of the pipeline

  21.       #!/bin/sh
  22.       x="this is the initial value of x"
  23.       while read line;do
  24.         x="$line"
  25.       done < dataFile
  26.       echo x = $x

  27.     With a Bourne shell you need to reassign file descriptors, so no
  28.     pipline or redirection in the loop is involved.

  29.       exec 3<&0         # save stdin
  30.       exec < file
  31.       while read line; do
  32.         x=$line
  33.       done
  34.       exec 0<&3        # restore stdin

  35.     Note that putting #!/bin/sh at the top of a script doesn't
  36.     guarantee you're using the Bourne shell. Some systems link /bin/sh
  37.     to some other shell. Check your system documentation to find out
  38.     what shell you're really getting in this case.
复制代码

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
6 [报告]
发表于 2008-05-09 13:44 |只看该作者

回复 #5 lgfang 的帖子

嗯, 这里也有讲
ftp://ftp.cwru.edu/pub/bash/FAQ 之E4

  1. E4) If I pipe the output of a command into `read variable', why doesn't
  2.     the output show up in $variable when the read command finishes?
复制代码

论坛徽章:
0
7 [报告]
发表于 2008-05-09 16:29 |只看该作者
烈火兄换头像了啊

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
8 [报告]
发表于 2008-05-09 16:43 |只看该作者
原帖由 mcolinc 于 2008-5-9 16:29 发表
烈火兄换头像了啊

嗯, 现在不是兴这个嘛~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP