3 ms·
Problems with this advice: ::looping over a file The "loop over the contents of a file" entry does not protect stdin. Use an alternate descriptor. while r
by chasil 4y ago
Problems with this advice:
::looping over a file
The "loop over the contents of a file" entry does not protect stdin.
Use an alternate descriptor.
while read -r line <&9
do printf '%s\n' "$line"
done 9< "$file"
Particularly problematic is ssh, that will pass $line to any remote command that is able to consume it.
The looped commands will receive stdin of the loop when an alternate descriptor is used. I use 9 by habit after reading the flock manual page many years ago.
::EXIT trap
dash will only call the EXIT trap on a normal exit. Add INT (and any other signal that you want from "kill -l") if you also want to catch abnormal terminations.
Windows busybox sh/bash only catches regular exits, no matter what else you add.
- ndsipa_pomu 4y agoYou might also want to clear IFS if you don't want to trim leading/trailing whitespace (taken from https://mywiki.wooledge.org/BashFAQ/001 https://mywiki.wooledge.org/BashFAQ/001): while IFS= read -r line <&9; do cat > ignoredfile printf '%s\n' "$line" done 9< "$file" ("cat" is used as an example of a command eating stdin)