ops102:bash_scripting_2_-_loops
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
ops102:bash_scripting_2_-_loops [2024/03/14 12:26] – created chris | ops102:bash_scripting_2_-_loops [2025/03/11 03:54] (current) – [tput Colour Codes] chris | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | |||
====== Bash Scripting 2: Loops ====== | ====== Bash Scripting 2: Loops ====== | ||
- | ===== Video ===== | ||
- | [[https://seneca-my.sharepoint.com/:v:/g/personal/chris_tyler_senecapolytechnic_ca/EVD6JDdK6TFCi52TQAiY6C8BdoDDANaEi8P1pt3TH4xH8w? | + | ===== Looping in Bash ====== |
+ | |||
+ | There are four types of loops in bash: | ||
+ | - '' | ||
+ | | ||
+ | - '' | ||
+ | - '' | ||
+ | |||
+ | In every case, the actual body of the loop is contained within '' | ||
+ | |||
+ | ==== for VARIABLE in LIST ==== | ||
+ | |||
+ | This type of loop iterates through a list of values; the //VARIABLE// is sequentially set to the each of the items in the list of values and the body of the loop is executed for each value. The list of values may be constants: | ||
+ | |||
+ | for X in 1 2 3 | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | for COLOUR in red green blue | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | Or the list may be the list of parameters (arguments to the script), written as ''" | ||
+ | |||
+ | for A in " | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | The most common use of this type of loop may be one or more globbing patterns (filename patterns with wildcard characters), | ||
+ | |||
+ | for SOURCEFILE in *.c | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | One thing to remember with this last form of the loop is that //if the pattern does not match any file// then the pattern itself will be assigned to the variable. For example, in the example above, if there are no files that match the pattern '' | ||
+ | |||
+ | This can be used in various ways. For example, this loop will compile all of the C source files found in the current directory: | ||
+ | |||
+ | for SOURCEFILE in *.c | ||
+ | do | ||
+ | echo "=== Compiling $SOURCEFILE ===" | ||
+ | |||
+ | # This next line removes the extension from the filename | ||
+ | BINARY=" | ||
+ | |||
+ | gcc SOURCEFILE -o $BINARY | ||
+ | done | ||
+ | |||
+ | (It's also possible to generate the //LIST// in other ways -- for example, from the output of another command, captured with '' | ||
+ | |||
+ | ==== The " | ||
+ | |||
+ | This loop is very similar to the " | ||
+ | |||
+ | For example, this loop counts from 1 to 10: | ||
+ | |||
+ | for (( i=0; i<=10; i++ )) | ||
+ | do | ||
+ | echo $i | ||
+ | done | ||
+ | |||
+ | ==== while EXPR ==== | ||
+ | |||
+ | This loop continues as long as the expression //EXPR// is true: | ||
+ | |||
+ | while [[ " | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | //EXPR// may be any single bash command, or a list of bash commands separated by newline characters (ENTER key) or semicolons, or a pipeline of commands. Most commonly it is a '' | ||
+ | |||
+ | ==== until EXPR ==== | ||
+ | |||
+ | This loop continues as long as the expression //EXPR// is false: | ||
+ | |||
+ | until [[ " | ||
+ | do | ||
+ | ... body of the loop ... | ||
+ | done | ||
+ | |||
+ | This loop will continue as long as '' | ||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | Here are a couple of examples from the lecture: | ||
+ | |||
+ | ==== tput Colour Codes ==== | ||
+ | |||
+ | The '' | ||
+ | |||
+ | The '' | ||
+ | |||
+ | # | ||
+ | |||
+ | for ((C=0; C<16; C++)) | ||
+ | do | ||
+ | tput setaf $C # Set foreground to colour $C | ||
+ | echo " | ||
+ | done | ||
+ | tput sgr0 # Reset to " | ||
+ | |||
+ | ==== Number-Guessing Game ==== | ||
+ | |||
+ | This is the main example from the lecture: | ||
+ | |||
+ | # | ||
+ | |||
+ | MAX=100 # Maximum value of secret | ||
+ | MAX_TRIES=7 # | ||
+ | GAMES=0 # How many games have been played | ||
+ | WINS=0 # How many games the user has won | ||
+ | |||
+ | clear # Clear the screen | ||
+ | tput setaf 5 # Select purple text | ||
+ | echo "=== Number-Guessing Game ===" | ||
+ | echo | ||
+ | echo "I have a secret number between 1 and $MAX" | ||
+ | echo "Your mission is to guess it in as few tries as possible." | ||
+ | echo "You have a maximum of $MAX_TRIES guesses to succeed." | ||
+ | echo | ||
+ | |||
+ | PLAY=" | ||
+ | # This next loop continues until the user says | ||
+ | # they don't want to play again | ||
+ | while [[ " | ||
+ | " | ||
+ | do | ||
+ | # Generate a random number from 1-MAX | ||
+ | SECRET=$(( RANDOM % MAX + 1 )) | ||
+ | |||
+ | # Un-comment this line when debugging! | ||
+ | # echo "NOTE: the secret number is $SECRET" | ||
+ | |||
+ | GUESS=0 | ||
+ | TRIES=0 | ||
+ | ((GAMES++)) | ||
+ | |||
+ | # This next loop continues until the user guess correctly | ||
+ | # or the maximum number of guesses is exhausted | ||
+ | until [[ $GUESS -eq $SECRET || $TRIES -ge $MAX_TRIES ]] | ||
+ | do | ||
+ | tput setaf 15 # White text | ||
+ | read -p "Enter your guess (# | ||
+ | if [[ $GUESS -gt $SECRET ]] | ||
+ | then | ||
+ | tput setaf 1 # Red text | ||
+ | echo "Too high!" | ||
+ | elif [[ $GUESS -lt $SECRET ]] | ||
+ | then | ||
+ | tput setaf 1 # Red text | ||
+ | echo "Too low!" | ||
+ | else | ||
+ | tput setaf 10 # Green text | ||
+ | echo "You got it in $((TRIES)) tries!" | ||
+ | ((WINS++)) | ||
+ | fi | ||
+ | echo | ||
+ | done | ||
+ | if [[ ! $GUESS -eq $SECRET ]] | ||
+ | then | ||
+ | tput setaf 11 # Yellow text | ||
+ | echo "You lose after $TRIES attempts. The number was $SECRET. " | ||
+ | fi | ||
+ | |||
+ | tput setaf 15 # White texxt | ||
+ | echo | ||
+ | read -p "Do you want to play again (Y/N)? " PLAY | ||
+ | echo | ||
+ | |||
+ | done | ||
+ | tput setaf 5 # Purple text | ||
+ | |||
+ | # In the next line, the multiplication | ||
+ | # must preceed the division because | ||
+ | # this is integer math | ||
+ | echo "You executed $GAMES missions and succeeded $WINS times ($((WINS*100/ | ||
+ | |||
+ | echo | ||
+ | tput sgr0 # Reset to normal text | ||
+ |
ops102/bash_scripting_2_-_loops.1710419160.txt.gz · Last modified: 2024/04/16 18:10 (external edit)