ops102:bash_scripting_2_-_loops
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
ops102:bash_scripting_2_-_loops [2024/03/14 13:12] – 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:// | ||
===== Looping in Bash ====== | ===== Looping in Bash ====== | ||
Line 59: | Line 56: | ||
(It's also possible to generate the //LIST// in other ways -- for example, from the output of another command, captured with '' | (It's also possible to generate the //LIST// in other ways -- for example, from the output of another command, captured with '' | ||
- | === The " | + | ==== The " |
This loop is very similar to the " | This loop is very similar to the " | ||
Line 70: | Line 67: | ||
done | done | ||
- | === while EXPR === | + | ==== while EXPR ==== |
This loop continues as long as the expression //EXPR// is true: | This loop continues as long as the expression //EXPR// is true: | ||
Line 81: | Line 78: | ||
//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 '' | //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 === | + | ==== until EXPR ==== |
This loop continues as long as the expression //EXPR// is false: | This loop continues as long as the expression //EXPR// is false: | ||
Line 92: | Line 89: | ||
This loop will continue as long as '' | This loop will continue as long as '' | ||
- | ==== Examples ==== | + | ===== Examples |
Here are a couple of examples from the lecture: | Here are a couple of examples from the lecture: | ||
- | === tput Colour Codes === | + | ==== tput Colour Codes ==== |
The '' | The '' | ||
Line 107: | Line 104: | ||
do | do | ||
tput setaf $C # Set foreground to colour $C | tput setaf $C # Set foreground to colour $C | ||
- | echo "This is colour | + | echo "Colour |
done | done | ||
tput sgr0 # Reset to " | tput sgr0 # Reset to " | ||
- | === Number-Guessing Game === | + | ==== Number-Guessing Game ==== |
This is the main example from the lecture: | This is the main example from the lecture: | ||
Line 140: | Line 137: | ||
SECRET=$(( RANDOM % MAX + 1 )) | SECRET=$(( RANDOM % MAX + 1 )) | ||
| | ||
- | # Un-commend | + | # Un-comment |
# echo "NOTE: the secret number is $SECRET" | # echo "NOTE: the secret number is $SECRET" | ||
| |
ops102/bash_scripting_2_-_loops.1710421930.txt.gz · Last modified: 2024/04/16 18:10 (external edit)