For loop bash shell scripting examples
Method 1: bash “for” loop using “in” and list of values
Syntax:
for varname in list
do
command1
command2
…..
done
In the above examples
- for, in, do and done are keywords
- “list” contains list of values. The list can be a variable that contains several words separated by spaces. If list is missing in the for statement, then it takes the positional parameter that were passed into the shell.
- Varname is any bash variable name.
Method2: bash “for” loop using C like syntax
The second form of the for loop is similar to the loop in “C” programming language, which has three expressions (initialization, condition and updation)
Syntax:
for (( expr1; expr2; expr3 ))
do
command1
command2
….
Done
In the above examples
- Before the first iteration, expr1 is evaluated. This is usually used to initialize variables for the loop.
- All the statement between do and done are executed repeatedly until the value of expr2 is TRUE.
- After each iteration of the loop, expt3 is evaluated. This usually used to increment a loop counter.
Example 1
Static values for the list after “in” keyword
[root@server1 Desktop]# vim useradd-for1.sh
#!/bin/bash
i=1
for user in ayyappan arun ravi alex aswin
do
useradd $user
echo password | passwd –stdin $user
done
Output
[root@server1 Desktop]# ./useradd-for1.sh
Changing password for user ayyappan.
passwd: all authentication tokens updated successfully.
Changing password for user arun.
passwd: all authentication tokens updated successfully.
Changing password for user ravi.
passwd: all authentication tokens updated successfully.
Changing password for user alex.
passwd: all authentication tokens updated successfully.
Changing password for user aswin.
passwd: all authentication tokens updated successfully.
Example 2
Variable for the list after “in” keyword
[root@server1 Desktop]# vim useradd-for2.sh
#!/bin/bash
i=1
names=”ayyappan arun ravi alex aswin”
for userlist in $names
do
useradd $userlist
echo password | passwd –stdin $userlist
done
Example 3
Don’t specify the list; get it from the positional parameters
[root@server1 Desktop]# vim useradd-for3.sh
#!/bin/bash
i=1
for userlist
do
useradd $userlist
echo “password” | passwd –stdin $userlist
done
Output
[root@server1 Desktop]# ./useradd-for3.sh ayyappan arun ravi alex
Changing password for user ayyappan.
passwd: all authentication tokens updated successfully.
Changing password for user arun.
passwd: all authentication tokens updated successfully.
Changing password for user ravi.
passwd: all authentication tokens updated successfully.
Changing password for user alex.
passwd: all authentication tokens updated successfully.
Example 4
Command output as list values after “in” keyword
[root@server1 Desktop]# vim userlist
ayyappan
arun
alex
ravi
ragu
:wq!
[root@server1 Desktop]# vim user.sh
#!/bin/bash
i=1
for username in `awk -F: ‘{print $1}’ /root/Desktop/userlist`
do
useradd $username
echo “password” | passwd –stdin $username
done
Example 5
Loop through /home directory user name remove in a for loop
[root@server1 Desktop]# cat user-remove.sh
#!/bin/bash
i=1
cd /home
for remove in *
do
userdel -rf $remove
echo “sucessfully removed user $remove “
done
OUTPUT
[root@server1 Desktop]# vim user-remove.sh
[root@server1 Desktop]# ./user-remove.sh
sucessfully removed user arun
sucessfully removed user ravi
sucessfully removed user ayyappan
Example 6
Printer numbers
[root@server1 Desktop]# cat random.sh
#!/bin/bash
echo “Enter the number : “
read num
for (( i=1; i <= $num; i++ ))
do
echo “$i”
done
OUTPUT
[root@server1 Desktop]# ./random.sh
Enter the number :
4
1
2
3
4
[root@server1 Desktop]# vim random.sh
#!/bin/bash
for num in {1..10}
do
echo “$num”
done
[root@server1 Desktop]# ./random.sh
0
2
4
6
8
10
Print ODD number only
[root@server1 Desktop]# cat odd.sh
#!/bin/bash
echo “Enter the ODD number range : ”
read num
for ((i=1; i<=$num; i=i+2 ))
do
echo “$i”
done
Print Even number only
[root@server1 Desktop]# cat even.sh
#!/bin/bash
echo “Enter the EVEN number range : ”
read num
for ((i=0; i<=$num; i=i+2))
do
echo “$i”
done
Print ODD and EVEN number only
[root@server1 Desktop]# cat ./odd-even.sh
#!/bin/bash
echo “Enter number range : ”
read num
for ((e=0, o=1; e<=$num, o<=$num; e=e+2, o=o+2))
do
echo “ODD Number: $o EVEN Number: $e”
done
OUTPUT
[root@server1 Desktop]# ./odd-even.sh
Enter number range :
5
ODD Number: 1 EVEN Number: 0
ODD Number: 3 EVEN Number: 2
ODD Number: 5 EVEN Number: 4
Print number
[root@server1 Desktop]# cat after-in.sh
#!/bin/bash
for num in {1..10}
do
echo “$num”
done
Print Even number
[root@server1 Desktop]# cat after-in.sh
#!/bin/bash
for num in {0..10..2}
do
echo “$num”
done
Print ODD number
[root@server1 Desktop]# cat after-in.sh
#!/bin/bash
for num in {1..10..2}
do
echo “$num”
done
Example 7
Print Infinitely number
[root@server1 Desktop]# cat sleep.sh
#!/bin/bash
i=1
for (( ; ; ))
do
sleep $i
echo “Number: $((i++))”
done
Note
“sleep” used for infinite number generate speed decreased.
“if” statement examples
1. if – then – fi
2. if – then – else – fi
3. if – then – elif – else – fi
4. if – then –else – if – then – fi – fi (nested if)
1. Bash if .. then .. fi statement
if [ conditional expression ]
then
statement1
statement2
fi
2. if [ conditional expression ]
then
statement1
statement2
else
statement3
statement4
fi
3. if [ conditional expression ]
then
statement1
statement2
elif [ conditional expression ]
then
statement1
statement2
else
statement1
statement2
fi
4. if [ conditional expression ]
then
statement1
statement2
else
if [ conditional expression ]
then
statement1
statement2
fi
fi
There are many different ways that an conditional statement can be used. These are summarized here:
String Comparison |
Description |
Str1 = Str2 | Returns true if the strings are equal |
Str1 != Str2 | Returns true if the strings are not equal |
-n Str1 | Returns true if the string is not null |
-z Str1 | Returns true if the string is null |
Numeric Comparison |
Description |
expr1 -eq expr2 | Returns true if the expressions are equal |
expr1 -ne expr2 | Returns true if the expressions are not equal |
expr1 -gt expr2 | Returns true if expr1 is greater than expr2 |
expr1 -ge expr2 | Returns true if expr1 is greater than or equal to expr2 |
expr1 -lt expr2 | Returns true if expr1 is less than expr2 |
expr1 -le expr2 | Returns true if expr1 is less than or equal to expr2 |
! expr1 | Negates the result of the expression |
File Conditionals |
Description |
-d file | True if the file is a directory |
-e file | True if the file exists (note that this is not particularly portable, thus -f is generally used) |
-f file | True if the provided string is a file |
-g file | True if the group id is set on a file |
-r file | True if the file is readable |
-s file | True if the file has a non-zero size |
-u | True if the user id is set on a file |
-w | True if the file is writable |
-x | True if the file is an executable |
The test command’s logical operators.
Command | Meaning |
! expr | Returns True if expr is not true. |
expr1 -a expr2 | Returns True if expr1 and expr2 are true. |
expr1 -o expr2 | Returns True if expr1 or expr2 is true. |
While loop format
Here is the basic format:
while [ condition ]
do
command1
command2
command3
done