Hi Everyone ,
Need a simple code here , I Have a number in a variable say $a=145.67 . This value changes everytime loop begins .
I need to print a specific message as shown below when the above variable lies in a specific range i.e.
1.if $a lies within 100 and 200 , it should display message1
2.if $a lies within 200 and 300 , it should display message2
if $a lies within 300 and 400 , it should display message 3 .
I Have a code which is not working :
if [$a -ge 100 ] && [$a -lt 200 ]
then
echo "message1"
elseif [$a -ge 200] && [$a -lt 300 ]
then
echo "message2"
elseif [$a -ge 300] && [$a -lt 400 ]
then
echo "message3"
fi
Note :
variable is a floating number .
would appreciate if someone could fix this or provide a better simple code .
One of the Error am getting :
a.ksh[5]: if[16783934.78: not found
+ echo 16783934.78
+ 1> mes.txt
a.ksh[7]: syntax error at line 8 : `else' unexpected
zaxxon
February 21, 2013, 4:50pm
2
As a start, put a blank between each square bracket and the variables. Try elif
instead of elseif
.
if [ $a -ge 100 ] && [ $a -lt 200 ]
then
echo "message1"
elif [ $a -ge 200 ] && [ $a -lt 300 ]
then
echo "message2"
elseif [ $a -ge 300 ] && [ $a -lt 400 ]
then
echo "message3"
fi
Indention of code is also a good thing to do.
Oh and forgot to point out, should use double square brackets together with >=, <= .... to be able to compare floating point values.
zaxxon:
As a start, put a blank between each square bracket and the variables. Try elif
instead of elseif
.
if [ $a -ge 100 ] && [ $a -lt 200 ]
then
echo "message1"
elif [ $a -ge 200 ] && [ $a -lt 300 ]
then
echo "message2"
elseif [ $a -ge 300 ] && [ $a -lt 400 ]
then
echo "message3"
fi
Indention of code is also a good thing to do.
Oh and forgot to point out, should use double square brackets together with >=, <= .... to be able to compare floating point values.
In recent versions of ksh, you can compare integer and floating point values with both [ expr ]
and [[ expr ]]
, but there are no >=
or <=
operators and <
and >
in [[ expr ]]
comparisons are for comparing string values, not arithmetic values.
The suggested changes zaxxon proposed in the code tags should work fine when using a ksh version newer than 1988, but keep the -ge
and -lt
rather than trying to use >=
and <
for arithmetic comparisons.
1 Like