Hello, Linux-fanatics, in our earlier tutorial we discussed advanced used of if-then-else conditional statements. In this tutorial, we will discuss for-conditional statement.

for statement is used where we require a set of commands to repeated over & over again until our conditions are all met. Like for example, we need to change some information for all users on system,

Syntax for for conditional statement 

for var (variable name) in list (parameters)
do
command (or commands)
done

Example Script

#!/bin/bash
# testing for statement
for name in dan susan daniel
do
echo “Welcome $name”
done
name=monti
echo “Ohhh, I forgot to welcome $name”

In this script, when for loop finishes last value of variable i.e. daniel, will be retained but we can reassign a new value i.e. monti.

Also, you should remember when using words like won’t or don’t , you should either escape those characters or use double quotation otherwise they will not be interpreted properly. For example:  won\’t don\’t   or “won’t” “don’t”

Now let’s take a look at another example

#!/bin/bash
# script to show use of double quotes & escape character
for var in I don\’t want to go to “North korea”
do
echo “ word : $var”
done

So in above script we escaped don’t & used double quotations with North Korea, now the output for the script will show proper result.Try yourself to write above script with & without escaping  the characters & using double quotation. You will notice the difference yourself.

Also, if we want that for statement reads from a variable  or use output of a command as list, we can also do that

Example Script

#!/bin/bash
# Reading list from a variable
name=”dan susan daniel”
name=$name“monti”
for member in $name
do
echo “Welcome again $member”
done

Here we defined a varaible "name" which we then used in for statement, also further in next line we added another namme to variable.

Now, let’s create a file named “name.txt” in “/home/dan” & lets write up some name there.  We will now use the file as our input for list in for statement

#!/bin/bash
# using a file as input
file=”/home/dan/name.txt”
for member in $(cat $file)
do
echo “Welcome $member ”
done

 

IMPORTANT NOTE: When you created your file, you might have listed names either by typing one name per line, or using single space between names or using tab as space between names.

Note that if you have commas to separate than all the names would  be interpreted as single name, why it is so. That’s because of environmental variable known as IFS or internal field separator and IFS interprets space, tab, newline as separators.

There might be a need  when we don’t want space or tabs to used as separators or we might want to add “,” comma as a separator. It can be done by using following in your script

IFS=’/n’,:

So, now only new line, comma & semi colon will be separators for the rest of the script.

Using for command in C language style

We can also for statement as is used in C programming language i.e.

for (( i=0; i=20; i++ ))

this will provide output of “I” starting from 1 until its equal to 20 with  increment of one.

With this approach you can also use multiple variable in single for statement

Example script

#!/bin/bash
#using multiple variables with C style for statement
for (( a=0, b=20; a =20 ; a++ b-- ))
do
echo “value of a is $a & value of b is $b”
done

Both ways of using for statement has merits and we can use them both depending on our need

Now that we have learned for conditional statement, let’s write a script with all conditional statement we have learned till now i.e. for-if-then-else

#!/bin/bash
# script using for-if-then-else (directory & file checking)
for file in /home/dan/*
do
if [ -d “$file”] then
echo”$file is a directory”
elif [ -f “$file”] echo “$file is a file not a directory”
if
done

So, with that script this tutorial is now complete. We are now entering into deep territories of scripting . Remember don’t try to over-learn in a single day, be patient & practice as much as you can.

And leave your questions/queries down below. ADIOS for now !!!

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter - Make a contribution via PayPal

[paypal_donation_button align="left" border="1"]

Linux TechLab is thankful for your continued support.