Thursday, 3 October 2013

Basic Linux Shell Scripting Language : Introduction to 'FOR' Loops


for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting, for-loops-in-linux-shell-scripting,

    Many times, it is necessary to execute a certain set of commands repeatedly for some number of times. For this purpose, one needs to use looping statements like 'For', 'While' and 'Until'. These statements run a block of code for multiple times.In this article, I will try to elaborate on Basic syntax of For loops with some examples of For loop usage.

Here we go!

The Well-Known For Loop

Most of us have learned a for loop with three expressions - Initialization, Control Condition, Iteration. In the same way, we can use these For loops in BASH Scripting.

Syntax:


for ((Initialization; Control Condition; Iteration))
do

    -- Block of Commands --

done

Example:


#!/bin/bash

for (( i=1; i<=5; i++ ))
do
   echo "This is Iteration No. $i"
done

Result:

This is Iteration No. 1
This is Iteration No. 2
This is Iteration No. 3
This is Iteration No. 4
This is Iteration No. 5

An Infinite Loop

You can also create a for loop that will execute for infinite number of times. For this, you just need to keep all the three expressions empty.

Example:

#!/bin/bash

for (( ; ; ))
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

For Loop with Numeric Ranges


Syntax:

for VAR in 1 2 3 .. N
do
    -- Block of Commands --
done
Example:

for i in 1 2 3 4 5
do
    echo "You are in Loop No. $i"
done
Result:

You are in Loop No. 1
You are in Loop No. 2
You are in Loop No. 3
You are in Loop No. 4
You are in Loop No. 5
    Instead of writing the whole series of numbers, you can also specify the range in the {START..END} format (END Inclusive). The variable will iterate through this range of numbers with a step value of 1 and the for loop will execute the block of commands for every number in the range.

Syntax:

for VAR in {START..END}
do
    -- Block of Commands --
done
Example:

for i in {1..5}
do
    echo "You are in Loop No. $i"
done
Result:

You are in Loop No. 1
You are in Loop No. 2
You are in Loop No. 3
You are in Loop No. 4
You are in Loop No. 5
    In the above example, the variable value increments by 1, by default, after each iteration. You can also specify the increment count as {START..END..INC_COUNT}.

Example:

for i in {0..15..3}
do
    echo "You are in Loop No. $i"
done
Result:

You are in Loop No. 0
You are in Loop No. 3
You are in Loop No. 6
You are in Loop No. 9
You are in Loop No. 12
You are in Loop No. 15

Using 'seq' Command

The seq command is used to generate a sequence of numbers. Go through the following syntaxes of the 'seq' command, they are self-explanatory.

'seq' Syntax:


seq END
seq START END
seq START INC END
Let us now create a For loop using 'seq' command.

Example:

#!/bin/bash
for i in $(seq 0 3 15)
do
   echo "You are in Loop No. $i"
done
Result:

You are in Loop No. 0
You are in Loop No. 3
You are in Loop No. 6
You are in Loop No. 9
You are in Loop No. 12
You are in Loop No. 15

'Break'ing the Loop

The break statements are used in the For, While and Until loops to exit from that loop. Conditional break statements are those which exits from the loop upon satisfying a certain condition.

Syntax:

for VAR in {RANGE}
do
    -- Some Commands --

    if [ condition ]
    then
        break
    fi

    -- More commands --
done
   Let us write a script which will search for the 'networking' file in the /etc/init.d directory.

#!/bin/bash

for file in /etc/init.d/*
do
    if [ "$file" == "/etc/init.d/networking" ]
    then
        echo "Found!"
        break
    fi
done
echo "Search Complete!"
    The above script will go through each and every file in the /etc/init.d directory. As soon as it finds 'networking' file therein, it will print 'Found!' and exit from the loop.

The 'continue' Statement

The continue statement in a loop, when certain condition becomes true, skips all the statements coming after it and continues with the next iteration of the loop.

Syntax:

for VAR in {RANGE}
do
    -- Some Commands --

    if [ condition ]
    then
        continue
    fi

    -- More commands --
done
Example:

for i in {1..20}
do
    j=$(expr $i % 2)

    if [ $j == 0 ]
    then
        continue
    fi

    echo "$i"
done
Result:

1
3
5
7
9
11
13
15
17
19
That's all about For Loops! If I missed something to write, please let me know in the comments. Stay tuned for more articles on Basic Linux Shell Scripting Language.

Also Read:

0 comments:

Post a Comment

    Total Pageviews