Bash Until Loop

Bash Until Loop

Loop is a fundamental concept in computer programming languages. This concept can be used in bash scripts as well when you want to execute multiple commands for more than one time.

The following three types of loops are available for use in bash scripts:

  1. for loop is used when you know in advance the exact number of times you want to execute the loop instructions (e.g., 10, 20, or 100 times).
  2. while loop is used when you don’t know how many times the loop will execute but you want to execute the instructions till the loop condition remains true.
  3. until loop is opposite of a while loop, as until loop will execute commands till the loop condition remains false.

Use the links to learn more about for and while loops.

In this tutorial, you will learn about until loop in greater detail along with its syntax and a few examples.

Until loop

Unlike the for loop, while and until loops are used when the number of times you want to execute commands is not known at run-time. Instead, it depends on the truth value of the loop condition. A while loop will keep on executing the commands till the loop condition remains true. When it becomes false the while loop will exit.

In contrast, an until loop will execute all instructions till the loop condition remains false. When the condition becomes true the until loop will exit.

Syntax

The syntax of an until loop is exactly the same as a while loop in a bash script, though it logically works completely opposite. The syntax of an until loop is mentioned below.

until [CONDITION]

do
  COMMANDS
done

The loop starts with the until keyword, followed by a condition (usually written in square brackets). Based on this condition, it is decided in each iteration whether to execute an until loop further or not. In other words, the loop will execute the commands repeatedly until the condition remains false. Once the condition becomes true, the until loop will terminate.

All the commands that you want to execute multiple times need to be enclosed in between do and done keywords.

Example 1

Let us start the explanation of Until loop using the following example.

hello.sh
#! /bin/bash

n=1

until [ $n -ge 10 ]
do
    echo "Number: $n"
    ((n++))
done

In the above example, a loop variable n is used, which is assigned an initial value of 1. Inside the until loop, a condition is specified within a square bracket. It is important to give spaces between all elements mentioned in the condition to avoid getting errors.

The value of n (which is initially 1) is compared with 10 using the -ge (greater than or equal to) option.

The echo command in the first line within the loop prints the value of n (obtained using $ sign). Whereas the second line increments the value of n by 1 (using C++ style). Remember to use double parenthesis here, otherwise, you will get an error.

After this line, the value of n is incremented by 1. This value when checked with the condition in the next iteration is still less than 10, so the loop condition becomes false and the loop will enter the second iteration. In this way, the loop will continue executing until the condition becomes true (means when the value of n becomes greater or equal to 1).

The output of the above until loop is shown below.

./hello.sh
Output
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9

If you change the parameter -ge with -gt, the above loop will execute one more time (because the value of n is equal to 10 and the condition “10 is greater than 10” is false). So, Number: 10 will also print.

Example 2

Have a look at another example to fully understand the working of an until loop.

hello.sh
#! /bin/bash

echo "Enter username"

read name

until [ "$name" == "Alex" ]
do
    echo "$name is not correct"
    break
done

The above script is started by asking the user to enter a name, which is stored in the variable called name using the read command. The entered username is tested in the loop condition. If it is not correct, the control will enter in the loop’s body and a message will print. If it is correct (meaning the condition will be true) the loop will exit and nothing would happen, as shown below.

When we input “david”
$ ./hello.sh
Enter username
david
david is not correct
When we input “alex”
$ ./hello.sh
Enter username
Alex

This example is a proof of concept that until loop only works when the condition is false. The break statement is used in the above example to exit the loop forcibly. As the above message is used in the body of the loop, it will be printed again and again, so that’s why a break statement is used to stop the loop.

A more practical Example

Let’s see another more practical example in the context of Linux Network administration.

Suppose that you want to check the connectivity with a server computer. Normally, for that, we use the ping command. In case the server computer is down, you have to issue the ping command again and again to check its status.

Since in this scenario a command has to be given repeatedly you can use a loop. Here you don’t know how many times you have to use the ping command, because you have no idea when the server would become up. Therefore, you can’t use a for loop.

Below is an example script, in which an until loop is used to ping a server repeatedly until the server starts giving any response.

#! /bin/bash

echo "Enter an IP address"

read ip

until ping -c 3 $ip
do
    echo "Host $ip is down"
    sleep 1
done

echo "Host $ip is up now"

The echo command is used to display a message to the user to enter the IP address to ping, the read command would take this input and store it in a variable named ip. Then, the value of this variable (means the IP address entered by the user) is used in the condition controlling the loop.

The ping command written as the condition of the loop will ping the given IP address 3 times (because of -c 3) and then display the “host down” message once and will wait for 1 second (due to sleep command set to 1). Due to the use of until loop, this process of pinging the server will continue as long as the given condition becomes false (means you are not getting any response of the ping command).

When you get any response (means the server is up and running), the condition becomes true and the loop will exit. Finally, a message will be displayed to the user that the Host computer is now up. The output of the above script is shown below.

./hello.sh
Output
Enter an IP address
192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2027ms

Host 192.168.1.1 is down
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

--- 192.168.1.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2042ms

Conclusion

Loops are used in Bash scripts to automate the tasks that are used repeatedly. There are three kinds of loops available (for, while, and until). Each has its advantages and they are used in particular situations. It’s up to you how wisely you can use these loops to automate the repeating tasks and save your time and effort.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Usermod Command in Linux
Read More

Usermod Command in Linux

The usermod command allows us to modify an existing user account. With the usermod command, we can make…