mirror of https://github.com/tLDP/LDP
53 lines
1007 B
Bash
53 lines
1007 B
Bash
#!/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
|
|
do {
|
|
echo "$line"
|
|
(( Lines++ )); # Incremented values of this variable
|
|
#+ accessible outside loop.
|
|
# No subshell, no problem.
|
|
}
|
|
done
|
|
exec 3>&-
|
|
|
|
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.
|