Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Vaardigheden in dit hoofdstuk
- Werk herhalen met while-, until- en for-lussen
Know what a loop is
A loop repeats a block of commands. Each round checks a condition: continue or stop.
Example from daily life: "Is there more dough? Then make one more cookie." The loop stops when the dough is gone.
Repeat with while
while runs the commands as long as the condition (an exit status) is 0 (true).
#!/bin/bash
# while-count: display a series of numbers
count=1
while [[ "$count" -le 5 ]]; do
echo "$count"
count=$((count + 1))
done
echo "Finished."while-count
1
2
3
4
5
Finished.Let op
If nothing in the loop changes the condition, it runs forever (an endless loop). Stop it with Ctrl-c.
Stop or skip with break and continue
break ends the loop at once. continue skips the rest of this round and starts the next one.
while true; do
read -p "Enter selection [0-3] > "
if [[ "$REPLY" == 0 ]]; then
break
fi
if [[ ! "$REPLY" =~ ^[0-3]$ ]]; then
echo "Invalid entry."
continue
fi
echo "You chose $REPLY."
done
echo "Program terminated."while trueis an endless loop on purpose;breakis the only way out.break 2andcontinue 2work on the outer loop of two nested loops.
Build a menu with select
select prints a numbered menu from a list, reads the choice into a variable, and repeats until break.
#!/bin/bash
PS3="Choose a report > "
select choice in "System info" "Disk space" "Quit"; do
case "$choice" in
"System info") uptime ;;
"Disk space") df -h ;;
"Quit") break ;;
*) echo "Invalid choice: $REPLY" ;;
esac
doneselect-menu
1) System info
2) Disk space
3) Quit
Choose a report > 2PS3is the prompt ofselect. The typed number is inREPLY, the chosen text in the variable (choice).
Repeat with until
until is the opposite of while: it repeats while the condition is false and stops when it becomes true.
#!/bin/bash
# until-count: display a series of numbers
count=1
until [[ "$count" -gt 5 ]]; do
echo "$count"
count=$((count + 1))
done
echo "Finished."Op het werk
until reads well for waiting: until ping -c1 server &> /dev/null; do sleep 5; done; echo "server is up".
Read a file with a loop
while read reads a file line by line. Redirect the file into the loop after done, or pipe a command into it.
#!/bin/bash
# while-read: read lines from a file
while read distro version release; do
printf "Distro: %s\tVersion: %s\tReleased: %s\n" \
"$distro" \
"$version" \
"$release"
done < distros.txtsort -k 1,1 -k 2n distros.txt | while read distro version release; do
printf "Distro: %s\tVersion: %s\tReleased: %s\n" \
"$distro" "$version" "$release"
donereadreturns a non-zero status at the end of the file, so the loop stops by itself.- With a pipe the loop runs in a subshell: variables set inside are lost after
done. - Safe pattern for any line:
while IFS= read -r line; do … done < file(keeps spaces and backslashes).
Summary of the chapter
while = repeat while true, until = repeat until true, break/continue change the flow, while read walks through a file.
1What is the difference between while and until?
while repeats as long as the condition is true; until repeats as long as it is false.
2How do you read /etc/hosts line by line without changing spaces or backslashes?
while IFS= read -r line; do echo "$line"; done < /etc/hosts.
3What does continue do?
It skips the rest of the current round and starts the next round of the loop.