1. 管道输出到read命令中, 使用管道echo输出来设置变量将会失败. 然而, 使用管道cat输出看起来可以正常运行. cat file1 file2 | while read linehtml
2 .while被放置在子shell中。linux
#!/bin/sh # readpipe.sh # Bjon Eriksson . 5 last="(null)" cat $0 | while read line do echo "{$line}" last=$line done printf "\nAll done, last:$last\n" 14 exit 0 # 代码结束. # 下边是脚本的(部分)输出. # 'echo'出了多余的大括号. 18 ############################################# 20 ./readpipe.sh 22 {#!/bin/sh} {last="(null)"} {cat $0 |} {while read line} {do} {echo "{$line}"} {last=$line} {done} {printf "nAll done, last:$lastn"} 32 All done, last:(null) 35 变量(last)被设置在子shell中, 并无被设置在外边. 在许多Linux发行版上, gendiff脚本一般都在/usr/bin下, 将find的输出通 过管道传到while read结构中. find $1 \( -name "*$2" -o -name ".*$2" \) -print | while read f; do . . .
3. 例外,在pipe中的一个大括号中的代码段可能运行在一个 子shell中 。shell
ls | { read firstline; read secondline; } echo "First line is $firstline; second line is $secondline" # 不能工做
4. 引用还能够改掉echo's不换行的"毛病".bash
bash$ echo $(ls -l) total 8 -rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh -rw-rw-r-- 1 bozo bozo 78 Aug 21 12:57 t71.sh bash$ echo "$(ls -l)" total 8 -rw-rw-r-- 1 bozo bozo 130 Aug 21 12:57 t222.sh -rw-rw-r-- 1 bozo bozo 78 Aug 21 12:57 t71.sh