15 For Loop - kumar159man/MyShellLearning GitHub Wiki

FOR loop

Basic shell script to demonstrate for loop

#!/usr/bin/bash
for i in 1 2 3 4 5
do
   echo "This is iteration $i"
done

Output

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./for1.sh 
This is iteration 1
This is iteration 2
This is iteration 3
This is iteration 4
This is iteration 5

Shell script to check execute permission for every file in a directory

#!/usr/bin/bash
if [[ $# -ne 1 ]]
then
     echo "You missed to enter the path as command line argument"
     exit
fi

for fileName in $(ls $1)
do
   [[ -x $fileName ]] && echo "$fileName has execute permission" || echo "$fileName do not have execute permission" 
done   

Same script can be implemented with if else block

#!/usr/bin/bash
if [[ $# -ne 1 ]]
then
     echo "You missed to enter the path as command line argument"
     exit
fi

for fileName in $(ls $1)
do
   if [[ -x $fileName ]]
   then
       echo "$fileName has execute permission"
   else
       echo "$fileName do not have execute permission"
   fi 
done   

C type FOR loop

for loop to print even numbers from 1-100

#!/usr/bin/bash
for((c=2;c<=100;c+=2))
do
   echo $c
done

Infinite for loop

#!/usr/bin/bash
for((;;))
do
   echo "This one is infinite for loop"
done

Usage of for loop to iterate with command line arguments

#!/usr/bin/bash
if [[ $# -eq 0 ]]
then
     echo "Please provide one command line arguments"
     exit 1
fi
     
for args in $*
do
   echo $args
done

For accessing command line arguments $@ also can be used instead of $*

Shell script to install multiple packages in ubuntu

#!/usr/bin/bash
if [[ $(id -u) -eq 0 ]]
then
     echo "You are running this script with root privillages"
else
     echo "Please run this script with root privillege"
     exit 1
fi

if [[ $# -eq 0 ]]
then
     echo "Please pass the name of packages as command line arguments"
     exit 2
fi
 
 
for pkgs in $*
do
   if which $pkgs &> /dev/null
   then
        echo "Package is already present"
   else
        echo "Installing Package......."
        apt install $pkgs -y &> /dev/null
        if [[ $? -eq 0 ]]
        then
             echo "Sucessfully installed $pkgs"
        else
             echo "There was some issue while installation of $pkgs"
        fi
   fi  
done

Difference between $* and $@

Let's have a simple script

#!/usr/bin/bash
echo "This will demonstrate \$*"
for args in $*
do
   echo $args
done

echo "This will demonstrate \$@"
for args in $@
do
   echo $args
done

First data set

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./diffcmdArgs.sh one two three four 
This will demonstrate $*
one
two
three
four
This will demonstrate $@
one
two
three
four

Second data set

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./diffcmdArgs.sh one two three four "a b" "1 2"
This will demonstrate $*
one
two
three
four
a
b
1
2
This will demonstrate $@
one
two
three
four
a
b
1
2

Both operations are same but whatever is under " " is taken as separate arguments

Let's modify the script and run with same dataset

#!/usr/bin/bash
echo "This will demonstrate \$*"
for args in "$*"
do
   echo $args
done

echo "This will demonstrate \$@"
for args in "$@"
do
   echo $args
done

Now Let's run the above script with same arguments

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./diffcmdArgs.sh one two three four "a b" "1 2"
This will demonstrate $*
one two three four a b 1 2
This will demonstrate $@
one
two
three
four
a b
1 2

"$@" will give exact command line arguments "$*" will convert every command line arguments to one command line arguments

⚠️ **GitHub.com Fallback** ⚠️