Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


write a shell script to find the largest number from 3 given
numbers.

Answers were Sorted based on User's Feedback



write a shell script to find the largest number from 3 given numbers...

Answer / vipul dalwala

if [ $# -ne 3 ]
then
echo "$0: number1 number2 number3 are not given" >&2
exit 1
fi

if [ $1 -eq $2 ] && [ $1 -eq $3 ]
then
echo "All the three numbers are equal"
elif [ $1 -eq $2 ] && [ $1 -gt $3 ]
then
echo "$1 and $2 numbers are equal and are largest
numbers among $1 $2 and $3"
elif [ $1 -eq $3 ] && [ $1 -gt $2 ]
then
echo "$1 and $3 numbers are equal and are largest
numbers among $1 $2 and $3"
elif [ $2 -eq $3 ] && [ $2 -gt $1 ]
then
echo "$2 and $3 numbers are equal and are largest
numbers among $1 $2 and $3"
elif [ $1 -eq $2 ] && [ $1 -lt $3 ]
then
echo "$3 is largest number among $1 $2 and $3"
elif [ $1 -eq $3 ] && [ $1 -lt $2 ]
then
echo "$2 is largest number among $1 $2 and $3"
elif [ $2 -eq $3 ] && [ $2 -lt $1 ]
then
echo "$1 is largest number among $1 $2 and $3"
elif [ $1 -gt $2 ] && [ $1 -gt $3 ]
then
echo "$1 is largest number among $1 $2 and $3"
elif [ $2 -gt $1 ] && [ $2 -gt $3 ]
then
echo "$2 is largest number among $1 $2 and $3"
elif [ $3 -gt $1 ] && [ $3 -gt $2 ]
then
echo "$3 is largest number among $1 $2 and $3"
else
echo "I can not figure out which number is
largest"
fi

Is This Answer Correct ?    22 Yes 19 No

write a shell script to find the largest number from 3 given numbers...

Answer / a

mr imran/vipul

when i run this script it always say number not given
what is the reason and how would this ask for the those 3 numbers to be sorted ??

Is This Answer Correct ?    3 Yes 0 No

write a shell script to find the largest number from 3 given numbers...

Answer / aseem

Hi Imran,

Could you please run your script with below parameter
4 45 1. It will give the output as "1 is the largest number"
reason is you have used only sort command not sort -nr so it will not sort in reverse order, so in your case head -1 will rather than tail -1.

Hi A,

Regarding your answer you have to run by below way.

say you are keeping your script in file test.sh
then

sh test.sh 43 1 34

Actually it takes the positional parameter so after script whatever input you provide it takes as parameter.

Feel free to send me email aseem02@gmail.com if you have any doubt regarding shell scripting, will glad to help you guys.

Is This Answer Correct ?    3 Yes 0 No

write a shell script to find the largest number from 3 given numbers...

Answer / aseem

Hi

"In continuation to above answer.

If you want to write a script in more generic way.

if [ $# -eq 0 ]
then
echo "$0: numbers are not given" >&2
exit 1
fi
LARGEST=$(echo $* | xargs -n1 | sort -nr | tail -1)
echo "$LARGEST is largest number""

The above script is good but there is a small msitake. As
numeric sort is used in reverse order so the biggest number
will come first, so we should use head -1 rather than tail -
1

Is This Answer Correct ?    9 Yes 8 No

write a shell script to find the largest number from 3 given numbers...

Answer / md. imran

if [ $# -eq 0 ]
then
echo "$: numbers are not given" >&2
exit 1
fi
LARGEST=$(echo $* |xargs -n1 |sort |head -1)
echo $LARGEST is largest number"

or if you want to take input from a file so type following
cat <filename>|xargs -n1|sort|head -1


Note : In first scipt mistake was in tail -1
right answer is head -1 which i have mentioned there.

thanks

Is This Answer Correct ?    5 Yes 5 No

write a shell script to find the largest number from 3 given numbers...

Answer / vipul dalwala

In continuation to above answer.

If you want to write a script in more generic way.

if [ $# -eq 0 ]
then
echo "$0: numbers are not given" >&2
exit 1
fi
LARGEST=$(echo $* | xargs -n1 | sort -nr | tail -1)
echo "$LARGEST is largest number"

Is This Answer Correct ?    13 Yes 15 No

Post New Answer

More Shell Script Interview Questions

What are the different shells available?

3 Answers  


What is mac default shell?

0 Answers  


What is shell and shell script?

0 Answers  


How to create environment variables?What are the conditions for creating variables?

1 Answers   Infosys, Wipro,


What is basename in shell script?

0 Answers  


if i have 2 files file1 and file2.... file1 contains 2 columns like b a 11 aa 12 as 13 ad 15 ag 11 ar 13 ah 15 ak file2 contains b c 10 ds 11 at 15 gh 15 jk 13 iu 11 fg 13 yy can any 1 give me the program to display in this way? a b c aa 11 at ar 11 fg ad 13 iu ah 13 yy ag 15 gh ak 15 jk

3 Answers  


What is path in shell script?

0 Answers  


what is the difference between cmp and diff commands

2 Answers   Amazon, CTS,


How would you compare the strings in a shell script?

0 Answers  


madhar chod unix ke 10 commands dhang se likh nahi sakta hai

0 Answers   Cap Gemini,


Explain about login shell?

0 Answers  


What is the need of including script interpreter in your shell script?

2 Answers  


Categories