help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Necessary and sufficient conditions for using file descriptor redirectio


From: Peng Yu
Subject: Necessary and sufficient conditions for using file descriptor redirection
Date: Tue, 30 Mar 2021 18:45:56 -0500

Hi,

https://tldp.org/LDP/abs/html/x17974.html

avoid-subshell.sh shows an example of why using file-descriptor redirection.

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>&-

But this example is not very good, as I can rewrite the code in this
way so that the redirection to a file descriptor is not needed.

while read line <&3
do {
  echo "$line"
  (( Lines++ ));                   #  Incremented values of this variable
                                   #+ accessible outside loop.
                                   #  No subshell, no problem.
}
done < myfile.txt

What are the necessary and sufficient conditions in which redirection
to file descriptor should be used? Thanks.

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]