LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   need correction for shell script (https://www.linuxquestions.org/questions/linux-newbie-8/need-correction-for-shell-script-465925/)

prospekrisal 07-20-2006 10:57 AM

need correction for shell script
 
even i enter 3 number this message is always displayed "Enter 3 number please"

here is the code


echo enter 3 numbers
read n1 n2 n3
if test "$n1" -o -z "$n2" -o -z "$n3"
then
echo "Enter 3 number please"
elif [ $n1 -ge $n2 -a $n1 -a $n3 ]
then
echo $n1 is maximum number
elif [ $n2 -ge $n1 -a $n2 -a $n3 ]
then
echo $n2 is maximum number
elif [ $n3 -ge $n1 -a $n3 -a $n2 ]
then
echo $n2 is maximum number
fi

druuna 07-20-2006 11:13 AM

Hi,

This:

if test "$n1" -o -z "$n2" -o -z "$n3"

Should be:

if test -z "$n1" -o -z "$n2" -o -z "$n3"

But there are some other 'strange' things:

$n1 -ge $n2 -a $n1 -a $n3 => Why the double -a check? Are you really checking if the could be files(-a file = True if file exists)?

I need to start assuming to give further advise, I'll just wait if you need any further advise.

Hope this helps.

prospekrisal 07-20-2006 11:34 AM

Quote:

Originally Posted by druuna

But there are some other 'strange' things:

$n1 -ge $n2 -a $n1 -a $n3 => Why the double -a check? Are you really checking if the could be files(-a file = True if file exists)?

Hope this helps.

it's wrong. I've fixed it. But I fount other strange things.. If I enter
3333 5 2563584 the ,maximum number is 5

how can?

druuna 07-20-2006 11:43 AM

Hi,

Yep found that out myself too. The logic is not correct. Here's a working version:

Code:

#!/bin/bash

echo "enter 3 numbers"
read n1 n2 n3

if [ -z "$n1" -o -z "$n2" -o -z "$n3" ]
then
  echo "Enter 3 numbers please."
elif [ $n1 -ge $n2 ] && [ $n1 -ge $n3 ] # Both must be true
then
  echo $n1 is maximum number
elif [ $n2 -ge $n3 ] # now we only need to check if n2 >= n3
then
  echo $n2 is maximum number
else # If we get here, n3 must be it.
  echo $n3 is maximum number
fi

If parts of the code are unclear, don't hesitate to ask.

prospekrisal 07-20-2006 12:03 PM

I got the logic,,,, thanksss


All times are GMT -5. The time now is 12:58 PM.