Chinaunix

标题: 求助!while read line的变量怎么传出来? [打印本页]

作者: yuhuohu    时间: 2012-03-09 11:00
标题: 求助!while read line的变量怎么传出来?
突然发现while read line循环里面的变量没法传出来,可我不想创建临时文件啊,各位有什么好招?
  1. -bash-4.1# a=123
  2. -bash-4.1# echo "xxx"|while read line; do a=$line; echo $a; done
  3. xxx
  4. -bash-4.1# echo $a
  5. 123
  6. -bash-4.1#
复制代码

作者: yinyuemi    时间: 2012-03-09 11:02
本帖最后由 yinyuemi 于 2012-03-09 11:05 编辑

回复 1# yuhuohu


    用命名管道,mkfifo

   或是 while ... done <file
作者: howge    时间: 2012-03-09 11:12
因为子shell的缘故你传不出来
作者: waker    时间: 2012-03-09 11:15
其实就是进程间通信
作者: bufferfly    时间: 2012-03-09 11:30
不要用管道(|),因为管道会导致循环在一个子shell中运行,用重定向好了。
作者: yuhuohu    时间: 2012-03-09 11:36
谢谢




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2