Practical FOR Loop Examples for Your UNIX Shell Script

We have studied basics of looping concepts in shell script here http://www.databaseetl.com/basics-unix-shell-scripting-part-2/.

Here we will see some practical examples of very often used FOR loop

There are three types of for loops available

  1. One using ‘in’ keyword with list of values
  2. One using like ((number=1; number < 100; number++)) which we used in C Programming.
  3. Range For loop

Let’s illustrate these methods/types with some practical examples.

As of now we already know that, loop executes for every value which is defined after keyword “in” i.e. variables. Now, it totally depends on us how we provide these values/variables i.e. provide variables through a separate file or list the variables directly after “in” keyword.

Example – 1: Variables declared right after “in”

$ cat loop1.sh

i=1

for Mon in Jan Feb Mar Apr May Jun

do echo “Month $((i++)): $Mon”

done

Lets run this loop1.sh script. There are 3 ways to execute the script as shown below

$ sh loop1.sh or ./loop1.sh or sh –x loop1.sh

Month 1: Jan

Month 2: Feb

Month 3: Mar

Month 4: Apr

Month 5: May

Month 6: Jun

Precaution 1 – Make sure, whenever you provide a list after “in” keyword then it should be separated with ‘space’ only i.e. space should be the delimiter.

Precaution 2 – Do not use comma ‘,’ as delimiter as it takes variable value as “Jan,” instead of “Jan”.

So our script output will be

Month 1: Jan,

Month 2: Feb,

Month 3: Mar,

Month 4: Apr,

Month 5: May,

Month 6: Jun

Or if variables declared in quotes (double or single) then it will be treated as a single value then our script output will be

Month 1: “Jan Feb Mar Apr May Jun”

Example 2: Variable saved in a list and called after “in” keyword

Here we will provide values through a variable declared outside for loop and call it in after “in” keyword as below.

$ cat loop2.sh

i=1

Month=”Jan Feb Mar Apr May Jun”

for Mon in $Month

do echo “Month $((i++)): $Mon”

done

$ sh for2.sh

Month 1: Mon

Month 2: Tue

Month 3: Wed

Month 4: Thu

Month 5: Fri

Example 3: Pass the variables as the arguments to the script

Here we won’t specify any list of variables or values. Will call these values while executing the script. Just need to careful here while writing the script, no need to mention ‘in’ keyword.

$ cat loop3.sh

i=1

for Mon


do echo “Month $((i++)) : $Mon”

done

$ sh loop3.sh Jan Feb Mar Apr May Jun

Month 1: Jan

Month 2: Feb

Month 3: Mar

Month 4: Apr

Month 5: May

Month 6: Jun

If you specify the ‘in’ keyword while writing the script then it will go into the loop but will get executed. And will return nothing.

Example 4: Break the loop

One can break the loop if you want to have only first 3 values,

$ cat loop3.sh

i=1

for Mon

do echo “Month $((i++)) : $Mon”

if [ $i -eq 4 ];

then

break;

fi

done

Output will be :

$ sh loop3.sh Jan Feb Mar Apr May Jun

Month 1: Jan

Month 2: Feb

Month 3: Mar

Example 5: C for Loop

As mentioned at the starting of the article, this is a another method where for loop has syntax similar to C for loop.

Consider below example where ‘i’ initialized as 1, loop will execute till it reaches to value 10, till it reaches 10 will be increased by 1.

for (( i=1; i <= 10; i++ ))

do

echo ” $i “

done

One can use two variables in a for loop as below

for ((i=1, j=100; i <= 5 ; i++, j=j+50))

Example 6 : Range for loop

One can provide range till which for loop will be executed in below format

for i in {1..5}

In above syntax/example, ‘i’ will be increased by 1 till 5

for i in {1..100..5}

in above for loop, I will be increased till 100 in the gap of 5 i.e. output will be 1, 6, 11, 16 …. 96


(Edit)

Leave a Reply

Logged as admin. Log out »

© 2017 Database ETL. All rights reserved.
Skip to toolbar Log Out