码迷,mamicode.com
首页 > 其他好文 > 详细

管道输出到read中遇到的问题

时间:2014-09-01 12:20:23      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   使用   div   问题   代码   log   sp   

1. 管道输出到read命令中, 使用管道echo输出来设置变量将会失败. 然而, 使用管道cat输出看起来能够正常运行.   cat file1 file2 |  while read line

2 .while被放置在子shell中。

1 #!/bin/sh
2 # readpipe.sh
3 #  Bjon Eriksson .
4
5 last="(null)"
6 cat $0 |
7 while read line
8 do
9 echo "{$line}"
10 last=$line
11 done
12 printf "\nAll done, last:$last\n"
13
14 exit 0 # 代码结束.
15 # 下边是脚本的(部分)输出.
16 # echo出了多余的大括号.
17
18 #############################################
19
20 ./readpipe.sh 
21
22 {#!/bin/sh}
23 {last="(null)"}
24 {cat $0 |}
25 {while read line}
26 {do}
27 {echo "{$line}"}
28 {last=$line}
29 {done}
30 {printf "nAll done, last:$lastn"}
31
32
33 All done, last:(null)
34
35 变量(last)被设置在子shell中, 并没有被设置在外边. 
在许多Linux发行版上, gendiff脚本通常都在/usr/bin下, 将find的输出通
过管道传到while read结构中.
1 find $1 \( -name "*$2" -o -name ".*$2" \) -print |
2 while read f; do
3 . . .

 

管道输出到read中遇到的问题

标签:style   blog   color   使用   div   问题   代码   log   sp   

原文地址:http://www.cnblogs.com/fly-xiang-zhao/p/3948874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!