This session focuses on automating repetitive tasks within shell scripts using various looping constructs available in Bash.
Learning Objectives
- Implement
forloops to iterate over lists, sequences, and file sets. - Utilize
whileloops for processing streams of data, such as reading files line-by-line. - Understand the application of
untilloops. - Learn flow control statements within loops (
breakandcontinue).
Topics Covered
1. The for Loop
- Iterating Over a List:
for item in item1 item2 item3; do ... done. - Iterating Over Sequences: Using brace expansion, e.g.,
for i in {1..10}orseq. - C-Style For Loop (Advanced):
bash
for ((i=0; i<10; i++)); do
echo "Iteration $i"
done
- File Globbing: Iterating directly over files matched by a pattern (e.g.,
for file in *.log; do ... done).
2. The while Loop
- Reading Input: The standard pattern for processing files:
while IFS= read -r line; do ... done < input_file. IFS=: Prevents word splitting.-r: Prevents backslash interpretation.- Conditional Loops: Using
whilewith a command whose exit status determines continuation (e.g.,while ping -c 1 google.com; do ... done). - Infinite Loops:
bash
while true; do
echo "System pulse check..."
sleep 5
done
3. The until Loop
- Inverted Condition: Executes as long as the command's exit status is non-zero (i.e., executes until the command succeeds).
4. Loop Control & Advanced Techniques
break: Exiting the loop immediately.continue: Skipping the remainder of the current iteration.- The
selectLoop (User Menus):
bash
PS3="Choose an option: "
select opt in "Update" "Status" "Quit"; do
case $opt in
"Update") sudo apt update ;;
"Status") uptime ;;
"Quit") break ;;
esac
done
Lab/Assessment Focus
Goal: Create an advanced bulk_rename.sh.
- Safety: Before renaming, use
[[ -w "$1" ]]to verify write permissions. - Iterate: Use a
forloop to find all.logfiles. - String Manipulation: Use
${file%.log}_backup.logto generate the new name. - Batching: Add a
whileloop that asks the user "Are you sure?" before each rename, unless the-yflag is passed as the second argument. - Menu: At the end, use a
selectloop to ask the user if they want to view the renamed files or exit.
Advanced Topic References
- Bash
while readLoop Best Practice: In-depth look at safe file reading. - Bash For Loop Variations: Comprehensive examples of
forloop usage. - Loop Flow Control in Bash: How to control loop execution.