Skip to main content

Command Palette

Search for a command to run...

While loop in shell scripts

Published
2 min read
While loop in shell scripts
M

Hi, How are you !! Hope you doing good....

I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective.

So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.

Last Blog Review

In the last blog we understood, few examples of shell script where for loop is used.

  1. Why while loops are required ?

Used when we have to execute a cmd or a set of cmds multiple times but you are not sure how many times, execute a cmd or set of cmds until a specific condition occurs, to create infinite loop, menu driven programs.

  1. Syntax

while
do
    ......
done
  1. Shell Script Example-1

    Write a script using while loop to print all the numbers from 1 to 3

     i-002978a646c30dd8b (EC2)
     -------------------------
     ----lp10:~$ vim print-numbers.sh 
     ----lp10:~$ cat print-numbers.sh 
     to_number=$1
     number=1
     while [ $number -lt $to_number ]
     do
          echo $(( number++ ))
     done
     ----lp10:~$ chmod 700 print-numbers.sh
     ----lp10:~$ ./print-numbers.sh 4
     1
     2
     3
    
  2. Shell Script Example-2

    Shows a menu driven program with the following options: Add, Subtract, Multiply, Divide, Quit

     i-002978a646c30dd8b (EC2)
     -------------------------
     ----lp10:~$ vim calculator.sh 
     ----lp10:~$ cat calculator.sh 
     #!/bin/bash
    
     while true
     do
       echo "1. Add"
       echo "2. Subtract"
       echo "3. Multiply"
       echo "4. Divide"
       echo "5. Quit"
    
       read -p "Enter your choice: " choice
    
       if [ $choice -eq 1 ]
       then
             read -p "Enter Number1: " number1
             read -p "Enter Number2: " number2
             echo Answer=$(( $number1 + $number2 ))
       elif [ $choice -eq 2 ]
       then
             read -p "Enter Number1: " number1
             read -p "Enter Number2: " number2
             echo Answer=$(( $number1 - $number2 ))
       elif [ $choice -eq 3 ]
       then
             read -p "Enter Number1: " number1
             read -p "Enter Number2: " number2
             echo Answer=$(( $number1 * $number2 ))
       elif [ $choice -eq 4 ]
       then
             read -p "Enter Number1: " number1
             read -p "Enter Number2: " number2
             echo Answer=$(( $number1 / $number2 ))
       elif [ $choice -eq 5 ]
       then
             echo "Thanks for using cal"
             break
       else
             echo "Please enter no. from 1 to 5"
             continue
       fi
    
     done
     ----lp10:~$ chmod 700 calculator.sh
     ----lp10:~$ ./calculator.sh 
     1. Add
     2. Subtract
     3. Multiply
     4. Divide
     5. Quit
     Enter your choice: 1
     Enter Number1: 5
     Enter Number2: 3
     Answer=8
     1. Add
     2. Subtract
     3. Multiply
     4. Divide
     5. Quit
     Enter your choice: 5
     Thanks for using cal
    

    Conclusion →

    In the this blog we understood, why WHILE LOOP is used and it’s syntax along with some examples of using WHILE LOOP.

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!

More from this blog

Mihir's Tech blog

69 posts