LDP/LDP/guide/docbook/abs-guide/avoid-subshell.sh

53 lines
1007 B
Bash
Raw Normal View History

2004-10-03 17:41:25 +00:00
#!/bin/bash
# avoid-subshell.sh
# Suggested by Matthew Walker.
Lines=0
echo
cat myfile.txt | while read line;
do {
echo $line
(( Lines++ )); # Incremented values of this variable
#+ inaccessible outside loop.
# Subshell problem.
}
done
echo "Number of lines read = $Lines" # 0
# Wrong!
echo "------------------------"
exec 3<> myfile.txt
while read line <&3
2004-10-03 17:41:25 +00:00
do {
echo "$line"
(( Lines++ )); # Incremented values of this variable
#+ accessible outside loop.
# No subshell, no problem.
}
done
exec 3>&-
2004-10-03 17:41:25 +00:00
echo "Number of lines read = $Lines" # 8
echo
exit 0
# Lines below not seen by script.
$ cat myfile.txt
Line 1.
Line 2.
Line 3.
Line 4.
Line 5.
Line 6.
Line 7.
Line 8.