Skip to main content
Bash ScriptingCentOS

UNTIL loops in BASH

By April 15, 2013September 12th, 2022No Comments

Looping in any scripting environment will be useful and fun. There are many forms of loops and in this tutorial we look at UNTIL loops. These loops are very similar to WHILE loops but we loop until a condition becomes true.

until test-expression
do
	commands
done

For our example the script will will monitor the size of a file, when it becomes greater than a set size we then stop the loop. We could use systems like this to check for the existence of a file and proceed, out of the loop once the file is there, or as in this case carry out an action when a file needs to be rotated.

#!/bin/bash
#This file was created on 23/02/13
COUNT=1
SZ=$(du -b /tmp/file1 | cut -f1 )
until (( SZ > 100 ))
do
        echo -e "$SZ c"
        echo "This is line $COUNT" >> /tmp/file1
        sleep 1
        (( COUNT ++ ))
        SZ=$(du -b /tmp/file1 | cut -f1 )

done
echo "file is greater than 100 bytes"