continue skips the rest of one turn. break ends the loop at once: the script goes on after done.
learner@reef:~$ cat ~/shop/specials.txt
# today's specials
apple pie
cheese scone
END
rhubarb tart
learner@reef:~$ cat specials.sh
#!/bin/bash
while IFS= read -r line; do
[[ "$line" == "#"* ]] && continue ← a comment: on to the next line
[ "$line" = END ] && break ← the end: stop reading
echo "$line"
done < ~/shop/specials.txt
echo "that's all"
learner@reef:~$ ./specials.sh
apple pie
cheese scone
that's all ← after break, the line after donerhubarb tart is never read.
Each register writes the day's sales, in cents, one per line:
After CLOSED, Ben entered two test sales: they don't count.
-
Write
~/register-total.sh FILE. It adds up the sales and printstotal: N. Skip comments and blank lines withcontinue. -
At
CLOSED,break. -
Make it runnable with
chmod u+x ~/register-total.sh, and run it:textlearner@reef:~$ ./register-total.sh ~/sales/north.txt total: 2775
Objectives
- Add up the sales. Skip comments and blank lines with
continue. - Stop reading at
CLOSEDwithbreak.