Exiting the loop with break and continue
This gives us nice opportunities, such as loops:
while true ; do echo "Hello" ; done
Since true
always as true, the is always verified so we have an infinite execution of the do/done
clause; press Ctrl+ C to exit from the loop. An infinite loop looks like something nasty, but it opens a new scenario for our scripts, since we can make them run or wait for something for as long as we want. Actually, if we do not use a couple of loop control commands: break
will exit the loop and continue
will restart it, jumping over the remaining commands. Let's see an example of creating a hypothetical backup program menu:
#!/bin/bash while true do clear cat <<MENU BACKUP UTIL v 1.0 ------------------ 1. Backup a file/directory 2. Restore a file/directory 0. Quit ------------------ MENU read -p "Please select an option, 0 or Q to exit: " option case $option in 1 | [Bb]) echo "You chose the first option, Backup" sleep 3 ;; 2 | ...