Everything Shell Script

  • Home
  • Home

Wednesday, 26 October 2016

While Loop in Shell Script

 Niraj Bhagchandani     02:23:00     No comments   

To run the given example live online please visit the following link: https://goo.gl/mD69Gj

There are basic loops in shell script.
  1. For Loop
  2. While Loop
  3. Until Loop
But, In this tutorial we are going to cover While loop.

BASH WHILE LOOP

The Syntax for while loop is as follows:

Syntax
while expression
do
           command
done


In the above syntax I am trying to explain three major components of while loop

  1. keywords - while, do and done
  2. Expression is any expression which returns scalar values
  3. The statement will execute all the commands between do and done until the condition or expression is true.
filename: while1.sh

#!/bin/sh
a=0
while [ $a -lt 10 ]              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


The another syntax for while loop can be termed in this following sequence.

while IFS= read -r line
do
     command 1;
     command 2;
     .
     .
     .
     command n;
done  < "path/to/dir/filename with or without space"


You may also write the following syntax.

while IFS= read -r field1 field2 field3 .... fieldN
do
         command1 on any one of the field
         command2 on any of the other fields.
         .
         .
         command3 on any of the fields
done < "path/to/dir/filename with or without space"


Here, IFS is used to set field separtor (by default the field seperator is white space). The -r option is to read command disables backslashes escaping such as \n,\t etc. This is failsafe while read loop for reading text files.

Here, IFS means Internal Field Separator. If time permits I will write a post for IFS too in future.

You may also use expression format in while Loop as.

filename: while2.sh
#!/bin/sh
a=0
while (( $a -lt 10 ))              #Spaces are very important here so keep that in mind.
do
         echo $a
         a=`expr $a + 1`
done


So, as we now know about the above loops let us take care of one more syntax which I have described above.
For that we need a file to read. Just look at the simplicity on how we can read the file in most efficient manner in Linux compared to C LANGUAGE.

Reading a text file using "space" as default field separator.

filename: while3.sh

#!/bin/sh
file="namefile"
while read -r f1 f2
do
      echo "Field 1 is: $f1 and Field 2 is: $f2"
done< "$file"



Reading a text file using separator field.

filename: while4.sh

#!/bin/sh
file="commafile"
while IFS="," read -r line
do
      echo $line
done < "$file"


Here, Internal Field Separator is set as "," with every comma it finds it will treat as a line and print its value.

Even though we have "," as a separator, the entire lines are printed because everytime it takes the entire line from the file and stores in in "line" variable.

Let us view one more example for IFS using ":" separator file. As we all know the information of all the users are stored in /etc/passwd file in linux.
So, Let us view that file using while loop

filename: while5.sh

#/bin/sh
file="/etc/passwd"
while IFS=: read -r user enpass uid gid desc home shell
do
    echo "User $user ($uid) it's home directory is $home with shell as $shell";
done < "$file"


With all lot of experience shared above, I can also term that different shell in linux have different while loop syntax.
Such as.

While Loop Syntax in ksh

while [[ condition ]]; do
    command-1
    command-2
    .
    .
    .
    command-n
done


While loop syntax in csh is as:

while ( condition )
    command
end;


Now, let us view the simple examples of the above syntax one by one.

KSH While Loop Example
filename: while6.sh 

#!/bin/ksh
index=1
while [[ $index -le 5 ]]; do
    echo "Welcome to e-shellscript: $index times";
    ((index++ ))
done


CSH While Loop Example

filename: while7.sh

#!/bin/csh
index=1
while ( $index <=5 )
    echo "Welcome to e-shellscript $index times";
    @ c = $c + 1
end


Let us view another example for c-shell

filename: while8.sh


#!/bin/csh
set yourname="eshellscript"
while ( $yourname != "" )
    echo -n "Enter your cute name: "
    set yourname = $<
    if ($yname != "" ) then
        echo "Hello , $yourname"
    endif
end
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Amazon Ads

Popular Posts

  • 12 differnet examples of For Loop in Shell Script
    The Link to programs given in this tutorial is:  https://goo.gl/akbcTw Well, to begin with FOR LOOP, I would tell that there are 2 diff...
  • Powerline – Adds Powerful Statuslines and Prompts to Vim Editor and Bash Terminal
    Powerline is a great statusline plugin for Vim editor , which is developed in Python and provides statuslines and prompts for many other...
  • How to install Docker and run Docker containers on Linux Mint 18/18.1
         How to install Docker and run Docker containers on Linux Mint 18/18.1 So, now before going through all the steps update and upg...
  • While Loop in Shell Script
    To run the given example live online please visit the following link: https://goo.gl/mD69Gj There are basic loops in shell script. For ...
  • Until Loop in Shell Script.
    To run these programs online I request you to go to following link: https://goo.gl/xKheU3 The until loop is similar to while loop in shel...
  • Variables in Shell Script - Part 1
      Every Programming language in existence has the concepts of variable – It can be termed as a chunk of memory which can be assigned valu...
  • Escape character in Shell Script
    Okay!! uptil now we have seen a lot of stuffs regarding Shell Script. But how about escape characters Example: What if you want to displa...
  • Wildcards
     A wildcard is a character that can be used as a substitute for any of a class of characters in a search, thereby greatly increasin...
  • My First Shell Script.
    My First Shell script We are about to write a script for Hello World. After that we will have some flavors of Hello World Program. Fir...

Recent Posts

Categories

  • Docker in Mint 18.1
  • Docker Linux Mint
  • Install Docker
  • Install Docker in Mint 18
  • Install Docker Linux Mint
  • tecmint

Pages

  • Home

Text Widget

Blog Archive

  • ►  2017 (1)
    • ►  June (1)
  • ▼  2016 (8)
    • ►  November (1)
    • ▼  October (7)
      • Until Loop in Shell Script.
      • While Loop in Shell Script
      • 12 differnet examples of For Loop in Shell Script
      • Escape character in Shell Script
      • Wildcards
      • Variables in Shell Script - Part 1
      • My First Shell Script.

Page Views:

About Me

Niraj Bhagchandani
View my complete profile

Search Here:

Powered by Blogger.

Sample Text

Copyright © Everything Shell Script | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates