Bash Until Loops

29/12/2020
There are several types of loops that can be used in bash scripts. For loops, while loops and until loops.

Conceptually the for loop should be used to loop through a series of items such as loop through each item in an array or each file in a directory, etc. The while loop should be used as long as a certain condition is true, such as the a counter is less than a maximum value or the ping time to a server is lower than a threshold or forever if you loop while TRUE or while 1.

The until loop is similar to the while loop but with reverse logic. Instead of looping while a condition is true you are assuming the condition is false and looping until it becomes true. They are reverse of each other in logical expression. Choosing the correct loop between a while loop and until loop allows your program to be more readable and understandable by others or yourself when you come back to the code sometime later.

Some typical examples or reasons to use a until loop could be, loop until the user enters ‘exit’; loop until the data generated is greater than the requested data volume, or until a number of files that match your search are found.

The basic syntax of UNTIL loop looks like this:

until [ CONDITION ]; do
  LINES OF CODE
  MORE LINES OF CODE
done

Now lets take some examples. The first example will multiple factor of two until reaching a size threshold of 1000:

#!/bin/bash
NUM=1
until [ "$NUM" -gt 1000 ]; do
  echo $NUM
  let NUM=NUM*2
done

The second example will continue to ping a URL until the response time is greater than 100 milliseconds:

#!/bin/bash
MILLISECONDS=0

# we will ping until it becomes slower than 1000 milliseconds
until [ $MILLISECONDS -gt 1000 ]
do
  # run the ping and extract the line that has the ping time, which ends in time=XXXX ms  
  OUTPUT=`ping -c 1 google.com | grep time | awk -F= ‘{ print $NF }’`
  echo "Ping time: $OUTPUT"

  # extract number of milliseocnds from string as integer
  MILLISECONDS=`echo $OUTPUT | awk ‘{ print $1 }’ | awk -F. ‘{ print $1 }’ `
  echo "Number of ms = $MILLISECONDS"

  sleep 1
done

echo "ping time exceeded 1000 milliseconds"

The third example will take a file and will combine the file with itself until it reaches 1 kilobyte in size:

#!/bin/bash
FILENAME=`basename "$0"`
echo $FILENAME
TMP_FILE="./tmp1"
TARGET_FILE="./target"
cat $FILENAME > $TARGET_FILE
FILESIZE=0

# increase file size until 1KB
until [ $FILESIZE -gt 1024 ]
do
  # add this file to target file content
  cp $TARGET_FILE $TMP_FILE
  cat $TMP_FILE >> $TARGET_FILE

  FILESIZE=`du $TARGET_FILE | awk ‘{ print $1 }’`
  echo "Filesize: $FILESIZE"

  sleep 1
done

echo "new filesize reached target of 1KB"

The fourth example will ask the user for input of their name until they type exit to quit the program:

#!/bin/bash
RESPONSE="FOO"

# increase file size until 1KB
until [ "$RESPONSE" = "exit" ]
do
  echo -n "Enter your name or ‘exit’ to quit this program: "
  read RESPONSE
  if [ "$RESPONSE" != "exit" ]; then
    echo "Hello $RESPONSE"
  fi
done

echo "Thank you for playing this game"

CONCLUSION

The key point is to use UNTIL loop to make your code more clear when the condition is expected to be always false and then you want to stop your looping action when the condition becomes true.  In other words, continue looping UNTIL some point in time.  With this perspective I hope your bash scripts can be more clear and you have learned something with this article. Thank you.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

30 Bash Script Examples

Bash scripts can be used for various purposes, such as executing a shell command, running multiple commands together, customizing...
28/12/2020

How to Set Environment Variables in Linux

Setting environment variables in Linux is a good way to define common and repetitive variables that are used across a number...
29/12/2020

Bash Split String Examples

We need to split the string data for various purposes in the programming. Many programming languages have a built-in function...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024