Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Probably a simple if then else solution out there 2

Status
Not open for further replies.

RFC1795

IS-IT--Management
Feb 13, 2003
76
US
Hi all,

I've tried to search the forums for a way to do this. The simple script bellow might explain more or less what I'm trying to do:

A="Option A - Value is less than or equal to 500"
B="Option B - Value is more than 501 but less than 5000"
C="Option C - Value is more than 5001"

if [ "$NUMB" -le "500" ]
then
echo $A
fi

if [ "$NUMB" -ge "500" ]
then
echo $B
fi

if [ "$NUMB" -le "5000" ]
then
echo $C
fi


I've tried a nested approach also using elif but it won't work for obvious reasons. If choosing a value such as 600 then it will match $B and $C

So really what I'm after is something or a way to say , if its more than 500 but less than 5000 then equal $B

Hope that makes sense :)

Thanks
 
What about this ?
if [ "$NUMB" -le "500" ]; then
echo $A
elif [ "$NUMB" -le "5000" ]; then
echo $B
else
echo $C
fi

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A="Option A - Value is less than or equal to 500"
B="Option B - Value is more than 501 but less than 5000"
C="Option C - Value is more than 5001"


it would seem that 501, 5000, and 5001 are to be excluded.
 
Thanks PHV ...

I can't believe I overlooked that ... I even use similar in other scripts already LOL

Thanks :)
 
or use a loop
Code:
while read num
do
  [[ $num -le 500 ]] { print $A; continue; }
  [[ $num -le 5000 ]] { print $B; continue; }
  print $C
done < file_with numbers
or, very similar
Code:
for num in $(command which produces numbers)
do
  [[ $num -le 500 ]] { print $A; continue; }
  [[ $num -le 5000 ]] { print $B; continue; }
  print $C
done

Ceci n'est pas une signature
Columb Healy
 
Thanks for the advice there .. it defiantly solved one problem and gave me another in the sed dept ;-)

Same script as example:


NUMB=$1
A="Option A - Value is less than or equal to 500"
B="Option B - Value is more than 501 but less than 5000"
C="Option C - Value is more than 5001"

if [ "$NUMB" -le "500" ]; then
echo $A
OPT="0 20 40 60"
elif [ "$NUMB" -le "5000" ]; then
echo $B
OPT="1 40 60 80"
else
echo $C
OPT="5 80 90 90"
fi

cat file.txt | sed s/VAR/$OPT/g


When I run it the sed is giving me a command garbled error:

./test12.sh 5001
Option C - Value is more than 5001
sed: command garbled: s/VAR/5


I'm guessing its something to do with the quoting but I'm missing the boat on it.

Any ideas?

BTW, the file.txt would contain something along these lines:
The values defined are within: VAR

Thanks
 
Code:
sed "s/VAR/$OPT/g" file.txt


Jean-Pierre.
 
Thanks Jean-Pierre .. that's exactly what I needed :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top