while IFS= read -r line reads the next line of a file on each turn, and stops at the end of the file.
continue skips the rest of a turn: the loop goes on with the next line.
whileIFS=keep spaces at the endsread -rkeep \ as typedlineone whole line; do … done< FILEthe file to read
text
learner@reef:~$ cat ~/shop/shifts.txt
# Who works when
mon ana
sat ana and ben
learner@reef:~$ cat shifts.sh
#!/bin/bash
while IFS= read -r line; do
[ -z "$line" ] && continue ← a blank line: on to the next
[[ "$line" == "#"* ]] && continue ← starts with #: a comment, skip it too
echo "[$line]"
done < ~/shop/shifts.txt
learner@reef:~$ ./shifts.sh
[mon ana]
[sat ana and ben] ← the whole line, as in the fileKeep the # in quotes: bare, it starts a comment, and the rest of the line is gone.
Ana keeps the shops' machines in ~/servers.txt, with comments:
- Write
~/hosts-only.sh FILE. It prints every line ofFILEthat is not blank and not a comment. - Make it runnable with
chmod u+x ~/hosts-only.sh, and run it on~/servers.txt.
Objectives
- Print the machine lines of
~/servers.txt, without comments and blank lines. - Read any file you give it, also one in a folder with a space.