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。
#!/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出管道两边的两个子进程,让后将一个子进程的输出重定向给另一个子进程的输入。
本文探讨了在bash脚本中,使用管道与重定向读取文件时,为何变量`Lines`的值在管道情况下不更新的问题。通过分析脚本执行的本质,解释了管道导致子进程隔离,使得变量变化不被父进程感知,而重定向则在同一个进程中进行,因此变量能够正确更新。

被折叠的 条评论
为什么被折叠?



