管道 无法改变变量值 的问题

wqfhenanxc@ubuntu:~$ cat test.sh
#!/bin/bash
Lines=0
cat $0 | while read line
do
  ((Lines++))
done
echo "Number of lines read is: $Lines"
exit 0
wqfhenanxc@ubuntu:~$ ./test.sh
Number of lines read is: 0

wqfhenanxc@ubuntu:~$ cat test.sh
#!/bin/bash
Lines=0
while read line
do
  ((Lines++))
done <$0
echo "Number of lines read is: $Lines"
exit 0
wqfhenanxc@ubuntu:~$ ./test.sh
Number of lines read is: 8

上面两个脚本一个使用了管道来读文件,一个使用了重定向来读。
结果为什么不一样呢?
请大家从脚本执行的本质上来给一下解答。

背景知识:
1.当我在命令行中输入./test.sh时,当前shell创建子shell。
   子shell读取test.sh中的命令,对于每一行命令,都先调用fork再调用exec。

2.对于管道,子shell先创建管道描述符,然后再fork出管道两边的两个子进程,让后将一个子进程的输出重定向给另一个子进程的输入。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章