Writing loop statement in Unix shell scripting with examples



Last updated on November 12th, 2016 at 08:02 am

What is loop statement in Unix shell scripting

A group of instruction that is executed repeatedly until a termination condition is met is called a loop.

Bash supports for and while loops. Some shell supports until also

unix shell script for loop example
How it works

In each and every loop, The variable used in loop condition must be initialized, then execution of the loop begins.
A test (condition) is made at the beginning of each iteration. The body of loop ends with a statement that modifies the value of the test (condition) variable. Loop is executed till the time, the test condition remain valid.

All the loops have a limited life and they come out once the condition is false or true depending on the loop.

A loop may continue forever due to required condition is not met. A loop that executes forever without terminating executes an infinite number of times. For this reason, such loops are called infinite loops

Lets take a look at different loop syntax

For Loop Unix shell

Syntax

The for statement
for command-list1
do
command-list2
done
Let start with one unix shell script for loop example

username=“Jat bob Thor Neil”

for uname in $username

do

echo “Username is $uname”

done

  1. do and done are reserved words

2. do and done must be preceded by a newline or ; (semicolon).

3 . Small loops can be written on a single line. For example:   for var in list; do commands; done

More unix shell script for loop example

To go through each file in the current directory and mention the filetype of each file

#!/bin/sh

for i in *

do

echo $i

file $i

done

While Loop

Syntax

The while statement
while command-list1
do
command-list2
done

Example

x=20

while [ $x >= 10 ]

do

echo “$x”

echo “\n”

sleep 5

x = x – 1

done

 

20

19

18

17

16

15

14

13

12

11

More examples

i=0

while [ $i -lt 5 ]

do

echo My id is $i

i=`expr $i + 1`

done

Until loop

Syntax

The until statement
until command-list1
do
command-list2
done

Example

until test -f $file

do

sleep 60

done

echo “$file now exists”

Nesting of Loops

We can do nesting of loops also

while test condition ; # this is loop1, the outer loop
do
Statement(s) to be executed if first test condition  is true

while test condition  ; # this is loop2, the inner loop
do
Statement(s) to be executed if test condition 2 is true
done

done

 

The break and continue statements in loops
It is often necessary to handle exception conditions within loops. The statements break and continue are used for this.

Break Continue
It is often necessary to handle exception conditions within loops. The statements break and continue are used for this.

The break command terminates the execution of the innermost enclosing loop, causing execution to resume after the nearest done statement.

To exit from n levels, use the command:

break n

This will cause execution to resume after the done n levels up.

The continue command causes execution to resume at the while, until or for statement which begins the loop containing the continue command

Examples for Continue

for filename in `cat filename.txt`

do

if [ ! -f $filenname ] ; then

continue

fi

echo “Found file $filename”

done

Examples for break

for filename in `cat filename.txt`

do

if [ ! -r $filename ] ; then

echo “Cannot read $filename, quitting loop”

break

fi

echo “Found file or directory $filename”

done

Related articles in Shell scripting

Unix Script Tutorial 1: What is shell and Shell Scripts

The Guide to Shell Scripting: The Basics

The guide to Shell Script: If statement

Hope you like the content  on Unix shell loops,unix shell script for loop example.  Please do provide the feedback


Leave a Reply