reworking content
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
This commit is contained in:
232
landing/docs/Bash-Scripts/011-bash-loops.md
Normal file
232
landing/docs/Bash-Scripts/011-bash-loops.md
Normal file
@@ -0,0 +1,232 @@
|
||||
# Loops
|
||||
|
||||
Loops are fundamental for automation. Bash provides `for`, `while`, and `until` loops to repeat commands efficiently.
|
||||
|
||||
---
|
||||
|
||||
### For Loops
|
||||
|
||||
The `for` loop iterates through a list of items.
|
||||
|
||||
```bash
|
||||
for var in ${list}
|
||||
do
|
||||
your_commands
|
||||
done
|
||||
```
|
||||
|
||||
Example: Loop through a list of users.
|
||||
|
||||
```bash
|
||||
touch for_users.sh
|
||||
```
|
||||
|
||||
Open `for_users.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
users="Dev Team Six softwareshinobi troy"
|
||||
|
||||
for user in ${users}
|
||||
do
|
||||
echo "${user}"
|
||||
done
|
||||
```
|
||||
|
||||
Save and exit. Make it executable and run:
|
||||
|
||||
```bash
|
||||
chmod +x for_users.sh
|
||||
./for_users.sh
|
||||
```
|
||||
|
||||
Output:
|
||||
```
|
||||
Dev Team Six
|
||||
softwareshinobi
|
||||
troy
|
||||
```
|
||||
|
||||
Loop through a range of numbers:
|
||||
|
||||
```bash
|
||||
touch for_numbers.sh
|
||||
```
|
||||
|
||||
Open `for_numbers.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for num in {1..10}
|
||||
do
|
||||
echo ${num}
|
||||
done
|
||||
```
|
||||
|
||||
Save, make executable, and run.
|
||||
|
||||
### While Loops
|
||||
|
||||
A `while` loop continues as long as its condition remains true.
|
||||
|
||||
```bash
|
||||
while [[ your_condition ]]
|
||||
do
|
||||
your_commands
|
||||
done
|
||||
```
|
||||
|
||||
Example: Counter from 1 to 10.
|
||||
|
||||
```bash
|
||||
touch while_counter.sh
|
||||
```
|
||||
|
||||
Open `while_counter.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
counter=1
|
||||
while [[ $counter -le 10 ]]
|
||||
do
|
||||
echo $counter
|
||||
((counter++)) # Increment counter
|
||||
done
|
||||
```
|
||||
|
||||
Save, make executable, run.
|
||||
|
||||
Example: Require user input. Loop until a non-empty name is provided.
|
||||
|
||||
```bash
|
||||
touch while_input.sh
|
||||
```
|
||||
|
||||
Open `while_input.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
read -p "What is your name? " name
|
||||
|
||||
while [[ -z "${name}" ]]
|
||||
do
|
||||
echo "Name cannot be blank. Please enter a valid name!"
|
||||
read -p "Enter your name again: " name
|
||||
done
|
||||
|
||||
echo "Hi there ${name}!"
|
||||
```
|
||||
|
||||
Save, make executable, run (test with empty and valid input).
|
||||
|
||||
### Until Loops
|
||||
|
||||
An `until` loop runs *until* its condition becomes true.
|
||||
|
||||
```bash
|
||||
until [[ your_condition ]]
|
||||
do
|
||||
your_commands
|
||||
done
|
||||
```
|
||||
|
||||
Example: Counter from 1 to 10.
|
||||
|
||||
```bash
|
||||
touch until_loop.sh
|
||||
```
|
||||
|
||||
Open `until_loop.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
count=1
|
||||
until [[ $count -gt 10 ]]
|
||||
do
|
||||
echo $count
|
||||
((count++))
|
||||
done
|
||||
```
|
||||
|
||||
Save, make executable, run.
|
||||
|
||||
### Continue and Break
|
||||
|
||||
Control loop flow with `continue` (skip current iteration) and `break` (exit loop entirely).
|
||||
|
||||
#### `continue`
|
||||
|
||||
```bash
|
||||
touch continue_example.sh
|
||||
```
|
||||
|
||||
Open `continue_example.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for i in 1 2 3 4 5
|
||||
do
|
||||
if [[ $i -eq 2 ]]; then
|
||||
echo "Skipping number 2"
|
||||
continue # Skip to next iteration
|
||||
fi
|
||||
echo "i is equal to $i"
|
||||
done
|
||||
```
|
||||
|
||||
Save, make executable, run.
|
||||
|
||||
#### `break`
|
||||
|
||||
```bash
|
||||
touch break_example.sh
|
||||
```
|
||||
|
||||
Open `break_example.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
num=1
|
||||
while [[ $num -lt 10 ]]; do
|
||||
if [[ $num -eq 5 ]]; then
|
||||
break # Exit loop
|
||||
fi
|
||||
((num++))
|
||||
done
|
||||
echo "Loop completed. Num stopped at: $num"
|
||||
```
|
||||
|
||||
Save, make executable, run.
|
||||
|
||||
For nested loops, `break N` exits `N` levels of loops. `break 2` exits the current and the parent loop.
|
||||
|
||||
```bash
|
||||
touch nested_break_example.sh
|
||||
```
|
||||
|
||||
Open `nested_break_example.sh` and add:
|
||||
|
||||
```bash
|
||||
#!/usr/bin/env bash
|
||||
|
||||
for (( a = 1; a < 3; a++ )); do # Outer loop
|
||||
echo "Outer loop: $a"
|
||||
for (( b = 1; b < 5; b++ )); do # Inner loop
|
||||
if [[ $b -gt 2 ]]; then
|
||||
echo " Breaking inner and outer loop at b=$b"
|
||||
break 2 # Exits both loops
|
||||
fi
|
||||
echo " Inner loop: $b"
|
||||
done
|
||||
done
|
||||
echo "All loops finished."
|
||||
```
|
||||
|
||||
Save, make executable, run.
|
||||
Reference in New Issue
Block a user