This article explains how to use looping constructs to automate rocket launches and streamline repetitive tasks in scripting.
In this lesson, we’ll explore how to use looping constructs to automate rocket launches. Previously, you built a script to launch a rocket for a single mission using one command. Now, imagine you need to launch hundreds of rockets for different missions across various celestial bodies. Instead of issuing commands manually for each mission, you can automate the process by repeatedly executing the same command with different mission names.
To streamline this process, consider creating a new script (e.g., launch_rockets.sh) that employs a for loop to run the create-and-launch-rocket command for each mission in a predefined list.
A for loop is especially useful when you need to run the same command repeatedly. Consider this initial version of a for loop:
Copy
Ask AI
for mission in lunar-mission jupiter-missiondo create-and-launch-rocket lunar-missiondone
Notice that the command inside the loop is hard-coded to lunar-mission. As a result, it will always launch the lunar mission regardless of the mission names listed. To ensure the correct mission is launched each time, replace the hard-coded value with the loop variable:
Copy
Ask AI
for mission in lunar-mission jupiter-mission saturn-mission satellite-mission lunar-mission-2do create-and-launch-rocket $missiondone
In this corrected version, the variable mission takes on each value from the provided list sequentially. For example, during the first iteration it will launch lunar-mission, during the second iteration jupiter-mission, and so forth.
When dealing with a large number of missions, manually listing all names in the script is impractical. Instead, store the mission names in an external file (e.g., mission-names.txt) and use command substitution to read from it:
Copy
Ask AI
for mission in $(cat mission-names.txt)do create-and-launch-rocket $missiondone
This approach decouples your data from the script logic. Any updates to the mission names can be managed by simply editing mission-names.txt, without needing to modify the script itself.
Use the modern syntax $(...) instead of backticks for command substitution. It is more readable and handles nested commands better.
For loops are not limited to rocket launches. They can significantly simplify repetitive tasks in various real-life scenarios, such as iterating over files, processing command outputs, or managing remote servers. Consider these examples:
Copy
Ask AI
# Loop through all files in the current directory and print the line count for each.for file in $(ls)do echo "Line count of $file is $(cat $file | wc -l)"done# Iterate over a list of servers provided in servers.txt and check their uptime via SSH.for server in $(cat servers.txt)do ssh $server "uptime"done# Install a list of packages stored in install-packages.txt.for package in $(cat install-packages.txt)do sudo apt-get -y install $packagedone
When using the SSH loop, ensure that your environment is configured for passwordless SSH access. Without this, you will be prompted to enter a password for each connection, which may hinder automation.
Using for loops in these ways not only simplifies repetitive tasks but also reduces the risk of manual errors, making your scripts more maintainable and user-friendly.