[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 21, 2023

What is loop and how to understand it.

What Is Loop And How To Understand It.

Looping Statements in Shell Scripting:

Three keywords to loop in Unix/Linux shell:

 

1.until

The statements in the until loop are executed until the command in the condition becomes true.

Syntax

The following is the syntax of the until loop:

until command
do
     Statement(s) to be executed
done

Code

The code below demonstrates the use of the until loop.

a=5

until [ $a -lt 1 ]   # a < 1
do
   echo $a  # print a
   a=`expr $a - 1`   # a = a - 1
done
Output
5
4
3
2
1

 

2.while

The statements in the while loop are executed until the command in the condition becomes false.

Syntax

The following is the syntax of the while loop:

while command
do
     Statement(s) to be executed
done

Code

The code below demonstrates the use of the while loop.

a=5

while [ $a -gt 0 ]   # a > 0
do
   echo $a  # print a
   a=`expr $a - 1`   # a = a - 1
done
Output
5
4
3
2
1

 

3.for

The for loop operates on a list of items. The statements in the for loop are executed for all items in the list.

Syntax

The following is the syntax of the for loop:

for var in word1 word2 ... wordN
do
   Statement(s) to be executed
done

Code

The code below demonstrates the use of the for loop.

for a in 5 4 3 2 1
do
  echo $a # print a
done
Output
5
4
3
2
1

 

Altering loops

We use the following keywords to alter the normal flow of a loop in Unix/Linux:

Code

The code below demonstrates the use of the break statement.

for a in 2 5 3 1 9 8 0 7 6 4
do
    if [ $a -eq 0 ]  # a == 0
    then
        break
    fi
    echo $a
done
Output
2
5
3
1
9
8

 

4.continue

We use the continue statement to terminate iteration(s) of the loop. It executes the line above continue and ignores all the lines below continue. It then starts execution from the start of the next iteration in the loop.

Code

The code below demonstrates the use of the continue statement.

for a in 1 2 3 4 5 6 7 8 9 10
do
    if [ `expr $a % 2` -eq 0 ]  # a % 2 == 0
    then
        continue
    fi
    echo $a
done
Output
1
3
5
7
9
Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM