13 April 2013

Shell Script to find Greatest of four numbers using If-Else-If Statement

12 comments

Q: How do I write Bash Script to find Greatest of Four Numbers using If-Else-If Statement


Ans:

#!/bin/bash

read -p "Enter first Number:" n1
read -p "Enter second Number:" n2
read -p "Enter third Number:" n3
read -p "Enter fourth Number:" n4

if((n1>n2)) ; then
 if((n1>n3)); then
   if((n1>n4)); then
     echo "$n1 is a Greatest Number"
   else
     echo "$n4 is a Greatest Number"
   fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
elif((n2>n3)); then
  if((n2>n4)); then
     echo "$n2 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
  fi
  elif((n3>n4)); then
     echo "$n3 is a Greatest Number"
  else
     echo "$n4 is a Greatest Number"
fi

Output


find greatest of four number using if-else-if statement in shell script

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

12 comments:

  1. This is really good but if all numbers are equal then what will happen?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. I don't think that I have an appropriate answer to that but I believe you can get help from the experts who provided me with a Corn Genetics Report Example which was very helpful to me.

    ReplyDelete
  4. This article is really contains lot more information about This Topic. We have read your all the information some points are also good and some usually are awesome. Great post I would like to thank you do my dissertation for me for the efforts you have made in writing this interesting and knowledgeable article.

    ReplyDelete
  5. I clearly stumbled upon your weblog and favored to mention that I’ve truely loved reading your blog posts. anyhow I’ll be subscribing in your feed and that i wish you submit Blade Runner Coat once more quickly. Please keeps it top posting! thanks you a lot, I recognize your work.

    ReplyDelete
  6. This article is truly contains part more data about This Topic. We have perused your all the data a few focuses are likewise acceptable and some normally are marvelous. Incredible post I might want to much obliged.boutique dresses online

    ReplyDelete
  7. Thank you for sharing good content. I found your blog on google search it’s very helpful for me.

    ReplyDelete
  8. Just Deliver Success is a T-shirt brand that delivers a powerful message with every shirt. Motivating individuals to achieve success through hard work and determination.

    ReplyDelete
  9. nasrene pimental, we pride ourselves on presenting you with innovative and extraordinary app ideas that hold immense potential for growth and success. Our team of experts has meticulously crafted these ideas to ensure that they not only meet your requirements but also exceed them.

    ReplyDelete
  10. A premier Branding Agency Dubai where creativity meets culture and innovation embraces tradition. This agency navigates the vibrant tapestry of Dubai's identity, weaving brands into its cosmopolitan fabric. With a fusion of modern strategies and Middle Eastern influences, they shape narratives that resonate globally. Elevating brands to iconic status, they embody the city's spirit of limitless possibilities.

    ReplyDelete
  11. The Write for Us Fashion program's promotion efforts across social media channels are a great incentive for contributors to reach a wider audience.

    ReplyDelete
  12. Writing Bash scripts can be tricky at first, but once you get the hang of the logic, it becomes straightforward! The if-else-if structure is perfect for comparing multiple numbers like this. By the way, if you're working late on this and need a break, Broadway Pizza Pakistan has some awesome deals! Their King Crust Beef Pizza is a personal favorite when burning the midnight oil. 😊

    ReplyDelete