Breaking News

By , Unix Dweeb, Network World |
The bash shell provides a superb functionality when it comes to writing scripts. This includes many ways to loop through a pile of data so that you can get a lot done with one running of a script. Whether you’re looping through a large group of numeric values, days of the week, usernames, words, files, or something else entirely, bash has an option that can make it easy for you.
The first thing you need to know about looping in bash is that there are several basic commands to use. The while loop will loop as long as some particular condition holds true. The until loop will loop until some condition becomes true, and the for loop will run through a series of values regardless of their origin.
For example, using while, you can loop while a number is smaller than 100, loop through the remaining days in the month as shown in the script below (in which we grab the current day using the date command and the final day from the last string in the output of the cal command) or loop through something altogether different.
When we run this script, we should see something like this:
This next script loops until a coworker logs in. As long as the count of the user’s logins is zero (i.e., he or she is not logged in), we display “waiting” and then wait another 60 seconds. Once the user is logged in, the while loop is exited and, within the next 60 seconds, a message is displayed confirming the user’s login presence.
The until loop version of the script is very similar.
In for loops, we run through a series of values, but there are many commands that you can use to generate those values.
The while and until commands are further explained in How to repeat a Linux command until it succeeds
You can loop through a string of numbers or letters by specifying a range of values in the {start..stop} format as in the examples below. Note that you can start with any value you want. You don’t have to start with an “a” or a “0”.
In this next example, we start with 5, not 1, and avoid the carriage returns with an echo -n command.
You can even reverse the order of the numbers or letters if you want. Here are a couple examples:
The easiest way to loop forever is to use a “while true” loop. The loop will only stop running if you kill it – for example, with a ^C. If it’s included in a script that is running in the background, you can use a kill command (e.g., kill 654321) to terminate the script.
More information on looping forever is available at How to loop forever in bash on Linux.
The break and continue commands allow you to jump out of a loop entirely (break) or restart at the top of the loop (continue), skipping over any commands between the continue command and the end of the loop.
The break and continue commands are explained in more detail at Using break and continue to exit loops.
The fold command can be used to split a file or string into same-size pieces. The default length is 80 characters. In this example, we break the string into 11-character chunks with the -c11 (11 characters) option.
When you use the fold command in a for loop, however, you need to remember that any resultant character string that contains a blank will result in one or more extra loops because for breaks on each blank-separated string.
More on the fold command can be found at Using fold to make text more readable and Using the fold command to drive loops.
To loop through a list of words using a for loop, you can run a command like the one below. Notice that the apostrophe needs to be escaped so as not to be interpreted as the beginning of a string.
If you want to loop through the months of the year, you can use a command like this:
The command above uses the locale command to get a list of months and sed to break the semi-colon separated line into individual months.
Looping in scripts and on the command line on Linux is clearly one of the best techniques for getting a lot done in a short amount of time and with the least amount of effort, especially if you turn your loops into scripts that you can run as often as needed without having to think through all the details each time.
Next read this:

  • 9 career-boosting Wi-Fi certifications
  • What is MPLS, and why isn’t it dead yet?
  • 11 ways to list and sort files on Linux
  • 5 free network-vulnerability scanners
  • How-to measure enterprise Wi-Fi speeds

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as “USL” (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she’s chasing the bears away from her bird feeders.
Copyright © 2022 IDG Communications, Inc.
Copyright © 2022 IDG Communications, Inc.

source

Leave a Reply

Your email address will not be published. Required fields are marked *

Share Article: