Skip to main content

Linux scripting: 3 how-tos for while loops in Bash

Three examples of using while loops to manage conditions that do not have a known limit.

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 for and while.

  • for loops are typically used when you have a known, finite list, like a series of numbers, a list of items, or counters.
  • while loops can be used with lists but are also useful for conditions that do not have a known limit. You can use it to run commands while a condition is true (or invert the logic and run it while the condition is false).

So, while (pun intended) there are different ways to perform loops, in this article, I focus on examples using while loops.

1. A simple while loop

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 df 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.

while true
do
  df -k | grep home
  sleep 1
done

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 sleep 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.

2. Waiting for a condition to become true

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 while 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:

#!/bin/bash

while [ ! -d directory_expected ]
do
   echo "`date` - Still waiting" 
   sleep 1
done

echo "DIRECTORY IS THERE!!!"

3. Using a while loop to manipulate a file

Another useful application of a while loop is to combine it with the read 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 /etc/hosts 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 while loops).

#!/bin/bash

cat servers.txt | grep -v CPU | while read servername cpu ram ip
do
   echo $ip $servername
done

[ Learn the basics of using Kubernetes in this free cheat sheet. ] 

Wrap up

while 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 while you can.

Check out these related articles on Enable Sysadmin

Topics:   Linux   Linux administration   Scripting  
Author’s photo

Roberto Nozaki

Roberto Nozaki [RHCSA/RHCE]  is a Senior Consultant at Red Hat Canada where he specializes in IT automation with Ansible. More about me

On Demand: Red Hat Summit 2021 Virtual Experience

Relive our April event with demos, keynotes, and technical sessions from
experts, all available on demand.

Related Content

OUR BEST CONTENT, DELIVERED TO YOUR INBOX