
“`html
The bash shell offers various methods to iterate through data, enhancing your experience, especially with scripting tasks.
You can cycle through a set of data to accomplish a lot by executing a single script, and you can create and iterate through distinct sequences of values. Whether you’re processing a large range of numerical values, the days of the week, usernames, terms, filenames, or any other category, bash provides a suitable option that simplifies the task.
The looping constructs available in bash consist of for loops, while loops, and until loops. This article presents examples of each option, from basic to somewhat advanced.
The most straightforward loop is likely a for loop, as illustrated below. It iterates as many times as there are items specified as arguments (in this particular case, 1, 2, 3, and 4). We could just as easily cycle through the phrase “cats are very smart” instead of the numbers 1, 2, 3, and 4.
“`
As mentioned earlier, loops aren’t limited to just numeric variables. They can also iterate through other types of variables, such as strings representing colors, states, planets, or even the days of the week. Here’s an example:
Once you execute this script and input your plans, the “week” file will serve as a reminder for how you plan to allocate each day of your week.
You can implement a loop similar to the ones illustrated below to go through the letters of the alphabet or a range of numbers.
The for command below will showcase calendars for the last three months of the current year.
The following script goes through each month of the year.
While loops continue to execute as long as the specified condition remains true. Here’s a demonstration:
In the previous example, the script increases and shows the value of $n as long as it is less than or equal to 4. You can create a while loop that runs indefinitely (i.e., until the system crashes, you log out, or someone terminates your script) by utilizing “while true” as illustrated below. The sleep command is included to prevent it from executing thousands of times before you have the chance to end it.
The script provided will keep running as long as the user it is monitoring has not yet logged into the system. This can be beneficial when you’re waiting for a co-worker to resolve an issue before you can continue with your task. It checks every minute to see if the user has logged in yet.
Until loops operate by continuing their execution until a specified condition becomes true. To achieve a similar result as the previous script, we can utilize an until command that runs until the count of the user being awaited reaches greater than 0.
Furthermore, there’s no restriction on nesting a loop within another loop. In this case, we’re demonstrating a for loop nested inside a while loop.
This script will iterate through the letters a, b, c, and d for the numbers ranging from 1 to 5.
The break and continue commands serve to modify the behavior of bash loops. The break command will terminate the loop, bypassing any subsequent instructions within it, while the continue command will skip the remaining commands and return to the loop’s beginning. In the script below, the break command is invoked, and the loop is concluded when the user correctly guesses a randomly chosen 1- or 2-digit number.
The for, while, and until loops can assist you in accomplishing a significant amount of work with minimal effort, providing a diverse array of options for configuring the range of values they will iterate through.
Welcome to DediRock, your trusted partner in high-performance hosting solutions. At DediRock, we specialize in providing dedicated servers, VPS hosting, and cloud services tailored to meet the unique needs of businesses and individuals alike. Our mission is to deliver reliable, scalable, and secure hosting solutions that empower our clients to achieve their digital goals. With a commitment to exceptional customer support, cutting-edge technology, and robust infrastructure, DediRock stands out as a leader in the hosting industry. Join us and experience the difference that dedicated service and unwavering reliability can make for your online presence. Launch our website.