Connect with us

Website

Understanding and Using the Bash Continue Statement

Published

on

Understanding and Using the Bash Continue Statement

Shell Scripting is a way to take shell commands that you could use from a normal command line and string them together allowing it to be run as a program allowing for automating repetitive tasks. Bash scripting is just one shell scripting language. Be aware that shell scripting is not the same as traditional programming languages, rather it’s one type of language called scripting languages. Scripting unlike traditional programming doesn’t need to be built/compiled after being created. Scripts are also typically less complex than full-blown software applications. In this blog post, we will touch on loops in bash scripting and move on to how bash continue statements can be used in loops.

What Is A Bash Loop?

In programming a loop is used to execute a specific code block repeatedly. During loop execution, the loop iterates over the information passed to it. The same goes for bash loops which allow for specific code to loop through to perform specified tasks.

The Bash Continue Statement

The bash continue statement is used within loops to skip the current iteration of the loop before the loop starts over again. It should be noted that the bash continue statement can only be used in for, while, and until loops.

Examples

The following example scripts include a mix of loops but all include the continue statement.

Wait For File To Exist

#!/bin/bash
until [ -f file.txt ]
    do
        continue
    done
echo "File found!"

This example waits till a file with the file name of file.txt is created and prints out a message.

Advertisement

Wait Until A Set Time Is Reached

#!/bin/bash
target_time=$(date -d "12:00" +%s)
until [ $(date +%s) -ge "$target_time" ]
    do
        sleep 1
        continue
    done
echo "Target time reached!"

This example waits for noon and then prints out a message.

Wait For The Ping To Fail

#!/bin/bash
while true; do
        if ping -c 1 google.com > /dev/null
        then
                continue
        else
                echo "Ping Failed check internet connectivity"
        fi 
        sleep 10
done

This example is a ping check that when fails prints out a message.

Did you notice this is also used nested loop? Although the original loop is a while loop there is a nested if loop inside it. From this point onward the rest of the examples have at least one nested loop.

Advertisement

List Files In the Correct Directory

#!/bin/bash
for f in $(ls); do
    if [ ! -f $f ]; then
        continue
    fi
    echo "File located: $f"
done

This example uses the ls command in the current directory and using the test command prints out the names of files.

Disk Usage Warning

#!/bin/bash
for dir in /home/*
do
        if [ ! -d "$dir" ]
        then
                continue
        fi
 
        disk_usage=$(du -sh $dir | cut -f1)
        if [ ${disk_usage%G} -gt 80 ]
        then
                echo "Disk usage for $dir is high: $disk_usage"
        fi
done

This example waits for disk usage to reach 80% and then prints out a warning.

Conclusion

When bash scripting loops are invaluable for automating repetitive tasks. The bash continue statement can only be used in loops. The continue statement skips the current iteration of the loop. The next iteration of the loop will then kick off and run through again testing the specified conditions.

Advertisement

If you creating a bash script with loops the continue statement may be just what you need to allow for better loop control.

Looking for more things to read why not check these links out?

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.