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

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.

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

echo "file is greater than 100 bytes"