Computing.Net > Forums > Unix > help with korn shell if command

help with korn shell if command

Reply to Message Icon

Original Message
Name: john k
Date: January 29, 2003 at 08:45:25 Pacific
Subject: help with korn shell if command
OS: UNIX
CPU/Ram: ALPHA, lots
Comment:

I am trying to read in 2 values from user input prompt, and if the second value is less than the first, I would like to re-prompt the user for value 2. I can't seem to get my statements to work correctly!
they are like:

echo "Enter low div: \c"
read div1

echo "Enter high div: \c"
read div2

if [ $div1 > $div2 ]
then
echo "high div is less than low div!!!"
echo "Please reenter high div: \c"
read div2
fi


The comparison always fails, no matter if the second value is larger than the first value! Any help would be appreciated! Thanks
John K



Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: January 29, 2003 at 11:44:27 Pacific
Reply: (edit)

#!/bin/sh
var1=10
var2=20
if [ $var1 -lt $var2 ] ; then
ans=`expr $var2 - $var1`
else
echo "Error"
fi
echo "Answer $ans"


Report Offensive Follow Up For Removal

Response Number 2
Name: WilliamRobertson
Date: January 29, 2003 at 15:21:49 Pacific
Reply: (edit)

Here it is in Korn shell syntax:

integer low high

while ! (( low high ))
do
read low?"Enter a number: "
read high?"Enter a higher number: "

(( low high )) || print -u2 "No, ${high} is not higher than ${low}. Please re-enter."
done

print Low: $low
print High: $high


Report Offensive Follow Up For Removal

Response Number 3
Name: WilliamRobertson
Date: January 29, 2003 at 23:21:55 Pacific
Reply: (edit)

Messageboard swallowed the less-than characters. That should be:

integer low high

while ! (( low high ))
do
read low?"Enter a number: "
read high?"Enter a higher number: "

(( low high )) || print -u2 "No, ${high} is not higher than ${low}. Please re-enter."
done

print Low: ${low}
print High: ${high}



Report Offensive Follow Up For Removal

Response Number 4
Name: WilliamRobertson
Date: January 29, 2003 at 23:25:49 Pacific
Reply: (edit)

...still no luck. Maybe this will do it:
(if not, there should be a 'less-than' symbol between 'low' and 'high' in the two conditions):

integer low high

while ! (( low \< high ))
do
read low?"Enter a number: "
read high?"Enter a higher number: "

(( low \< high )) || print -u2 "No, ${high} is not higher than ${low}. Please re-enter."
done

print Low: ${low}
print High: ${high}


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: help with korn shell if command

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge