Geavanceerd
Zoeken inSamenvattingen
Zo zoek je
  • Woorden vinden lestitels, tekst en code. Filter op soort (gidsen, cursussen, programma's, boeken) of blijf in dit boek.
  • book:rhcsa en ch:8 maken de zoektocht kleiner; #code vindt een woord alleen in commando's van de lessen.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Hoofdstuk 29Naam in het boek: Flow Control: Looping with while / until

Loop with while and until

Much daily work is repetition: do it again until it is done. Loops repeat commands. This chapter covers while (repeat while a condition is true), until (repeat until it becomes true), break and continue, the select menu, and reading a file line by line.

  • 8 Lessen
  • 1 Commando's
  • 1 Labs

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

29.1

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.

29.2

Repeat with while

while runs the commands as long as the condition (an exit status) is 0 (true).

while-count
#!/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.

29.4

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.

an endless loop that ends with break
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 true is an endless loop on purpose; break is the only way out.
  • break 2 and continue 2 work on the outer loop of two nested loops.

29.5

Build a menu with select

select prints a numbered menu from a list, reads the choice into a variable, and repeats until break.

select-menu
#!/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
done
select-menu
1) System info
2) Disk space
3) Quit
Choose a report > 2
  • PS3 is the prompt of select. The typed number is in REPLY, the chosen text in the variable (choice).

29.6

Repeat with until

until is the opposite of while: it repeats while the condition is false and stops when it becomes true.

until-count
#!/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".

29.7

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.

while-read: read lines from a file
#!/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.txt
the same with a pipe
sort -k 1,1 -k 2n distros.txt | while read distro version release; do
    printf "Distro: %s\tVersion: %s\tReleased: %s\n" \
        "$distro" "$version" "$release"
done
  • read returns 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).

Commando's in deze les

29.8

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.

Oefenen

Alle commando's in dit hoofdstuk