Shell Script Program To Calculate The Area Of Rectangle And Circle

In this article, we calculated the area of rectangle and circle using shell script. To calculate the area of rectangle we simply multiply length and breadth and for area of circle we multiply pi value with radius.

Shell script program to calculate the area of rectangle and circle

So get started!We write two statement for user indicating area of rectangle and area of circle.In addition take choice of user whether he wants to find area of rectangle or circle by using read command.

To do branching we requires case statement in shellscript. We used two case statement one for finding rectangle and other for circle.

In case of rectangle, take input length and breadth from user while in circle take radius
Apply the formula of finding rectangle and circle and print it using echo command.

Check the output image for how to execute shell script program to find area of rectangle and circle

echo \”===========FINDING AREA OF CIRCLE AND RECTANGLE==============\”

echo \”1) Area of Rectangle\”

echo \”2) Area of Circle\”

echo \”Enter the choice\”

pi=3.14

read ch

case $ch in  

 1)  echo -n \”Entre Length of rectangle\”        

  read a  

echo -n \”Enter breadth of rectangle\”        

  read b     

 rarea=`expr $a \\* $b`

echo \”Area of  rectangle is $rarea\” 

  break

;;

2)

echo -n \”Enter radius of circle\”    

  read r    

  rsq=$(( $r*$r))  

   carea=`echo \”$pi*$rsq\” |bc`  

   echo \”Area of circle is $carea\”   

  break;

;;

Output of shell script to find area of circle and rectangle

area of circle

FAQ About Shell Script Program for finding Area of Rectangle

How do I search in shell script?

To search a string in a shell script, open your Linux terminal and press ctrl+r multiple times. for example you want to search specific command from the listed commands in terminal

How to use grep in shell script?

Use grep command to search string or pattern in file using the simple grep command like grep “string” filename.

How to write script in shell?

Shell script are written using text editor like “nano”. if you’re using Linux system like Ubuntu then open nano editor ,add some script, open the Linux terminal and execute your first script.

Leave a Comment

Your email address will not be published.