Linux scripting: 3 how-tos for while loops in Bash - 4 minutes read
Shell scripting, specifically Bash scripting, is a great way to automate repetitive or tedious tasks on your systems. Why type when you can schedule and run scripts in a hands-free manner? One of the many scripting constructs is the loop. A loop is a section of code that picks up data or generates data and then performs some operation on the data and then begins the process over again until some condition is met, the script is disrupted, or when input data is exhausted. When you use Bash scripting, sometimes it is useful to perform actions using some form of loops.
[ Readers also liked: Five ways to use redirect operators in bash ]
The basic loop commands in Bash scripts are and .
So, while (pun intended) there are different ways to perform loops, in this article, I focus on examples using loops.
Imagine that you're installing a huge application, and you're concerned that it may eventually use too much space on the file system.
Instead of running in a separate terminal, you can run a simple command to monitor the disk utilization every second. This allows you to interrupt the installation process if you see that it will hit that limit.
In this case, you're running the loop with a true condition, which means it will run forever or until you hit CTRL-C. Therefore, you need to keep an eye on it (otherwise, it will remain using the system's resources).
Note: If you use a loop like this, you need to include a command like to give the system some time to breathe between executions. Running anything non-stop could become a performance issue, especially if the commands inside the loop involve I/O operations.
There are variations of this scenario. For example, you know that at some point, the process will create a directory, and you are just waiting for that moment to perform other validations.
You can have a loop to keep checking for that directory's existence and only write a message while the directory does not exist.
If you want to do something more elaborate, you could create a script and show a more clear indication that the loop condition became true:
Another useful application of a loop is to combine it with the command to have access to columns (or fields) quickly from a text file and perform some actions on them.
In the following example, you are simply picking the columns from a text file with a predictable format and printing the values that you want to use to populate an file.
Here the assumption is that the file has columns delimited by spaces or tabs and that there are no spaces in the content of the columns. That could shift the content of the fields and not give you what you needed.
Notice that you're just doing a simple operation to extract and manipulate information and not concerned about the command's reusability. I would classify this as one of those "quick and dirty tricks."
Of course, if this was something that you would repeatedly do, you should run it from a script, use proper names for the variables, and all those good practices (including transforming the filename in an argument and defining where to send the output, but today, the topic is loops).
[ Learn the basics of using Kubernetes in this free cheat sheet. ]
loops are useful in scripts that depend on a certain condition to be true or false, but you can also use them interactively to monitor some condition.
The important things to remember are:
Always be clear about what is the condition to end the loop. If that is something that you expect to do yourself with a CTRL-C, then keep an eye on your command. Think about how fast you want each loop cycle to be repeated. If you do any operation that involves I/O inside the loop, that becomes a critical aspect of planning.
With that in mind, have fun you can.
Source: Redhat.com
Powered by NewsAPI.org