The break statement is used to exit the current loop before its normal ending. This is done when you don't know in advance how many times the loop will have to execute, for instance because it is dependent on user input.
The example below demonstrates a while loop that can be interrupted. This is a slightly improved version of the wisdom.sh script from .
#!/bin/bash # This script provides wisdom # You can now exit in a decent way. FORTUNE=/usr/games/fortune while true; do echo "On which topic do you want advice?" echo "1. politics" echo "2. startrek" echo "3. kernelnewbies" echo "4. sports" echo "5. bofh-excuses" echo "6. magic" echo "7. love" echo "8. literature" echo "9. drugs" echo "10. education" echo echo -n "Enter your choice, or 0 for exit: " read choice echo case $choice in 1) $FORTUNE politics ;; 2) $FORTUNE startrek ;; 3) $FORTUNE kernelnewbies ;; 4) echo "Sports are a waste of time, energy and money." echo "Go back to your keyboard." echo -e "\t\t\t\t -- \"Unhealthy is my middle name\" Soggie." ;; 5) $FORTUNE bofh-excuses ;; 6) $FORTUNE magic ;; 7) $FORTUNE love ;; 8) $FORTUNE literature ;; 9) $FORTUNE drugs ;; 10) $FORTUNE education ;; 0) echo "OK, see you!" break ;; *) echo "That is not a valid choice, try a number from 0 to 10." ;; esac done
Mind that break exits the loop, not the script. This can be demonstrated by adding an echo command at the end of the script. This echo will also be executed upon input that causes break to be executed (when the user types "0").
In nested loops, break allows for specification of which loop to exit. See the Bash info pages for more.
The continue statement resumes iteration of an enclosing for, while, until or select loop.
When used in a for loop, the controlling variable takes on the value of the next element in the list. When used in a while or until construct, on the other hand, execution resumes with TEST-COMMAND at the top of the loop.
In the following example, file names are converted to lower case. If no conversion needs to be done, a continue statement restarts execution of the loop. These commands don't eat much system resources, and most likely, similar problems can be solved using sed and awk. However, it is useful to know about this kind of construction when executing heavy jobs, that might not even be necessary when tests are inserted at the correct locations in a script, sparing system resources.
[carol@octarine ~/test] cat tolower.sh #!/bin/bash # This script converts all file names containing upper case characters into file# names containing only lower cases. LIST="$(ls)" for name in "$LIST"; do if [[ "$name" != *[[:upper:]]* ]]; then continue fi ORIG="$name" NEW=`echo $name | tr 'A-Z' 'a-z'` mv "$ORIG" "$NEW" echo "new name for $ORIG is $NEW" done
This script has at least one disadvantage: it overwrites existing files. The noclobber option to Bash is only useful when redirection occurs. The -b option to the mv command provides more security, but is only safe in case of one accidental overwrite, as is demonstrated in this test:
[carol@octarine ~/test] rm * [carol@octarine ~/test] touch test Test TEST [carol@octarine ~/test] bash -x tolower.sh ++ ls + LIST=test Test TEST + [[ test != *[[:upper:]]* ]] + continue + [[ Test != *[[:upper:]]* ]] + ORIG=Test ++ echo Test ++ tr A-Z a-z + NEW=test + mv -b Test test + echo 'new name for Test is test' new name for Test is test + [[ TEST != *[[:upper:]]* ]] + ORIG=TEST ++ echo TEST ++ tr A-Z a-z + NEW=test + mv -b TEST test + echo 'new name for TEST is test' new name for TEST is test [carol@octarine ~/test] ls -a ./ ../ test test~
The tr is part of the textutils package; it can perform all kinds of character transformations.
Meet new people