Help with nested "if"

Hi, i just wanna ask for some help with this

while true
do
  clear
  date
  echo AIX 630 AMOUNT OF FEPs
VAR=`ps -eaf | grep fep2 | grep cajaBMSz | wc -l`


{
 if [ $? -gt 10 && -le 20 ]
 then
    if [ $? -ge 21 ]
     then
        printf "\033[01;31m%s\033[00m\n" "$VAR"
     else
        printf "\033[01;33m%s\033[00m\n" "$VAR"
    fi
 else
        printf "\033[01;32m%s\033[00m\n" "$VAR"
 fi
}

done;

What i want is this.. if the value returned by

ps -eaf | grep fep2 | grep cajaBMSz | wc -l

is between the values i suggested the script must output the colors i selected... but instead i get the "mon55z[22]: test: 0403-021 A ] character is missing." message. mon55z is the name of my script BTW...

thanks in advance

Take a look at

 if [ $? -gt 10 && -le 20 ]

I think

 if [ $? -gt 10 && $? -le 20 ]

However, how can something be gt 10 and lt 20
with your next command checking for it gt 21?

The && is for and, || is for or.

1 Like

Why are you using the special shell variable $? that holds the exit status of last command executed?

That is not correct. It will be zero if the command executed successfully otherwise non-zero.

Use the variable VAR instead:

if [ $VAR -gt 10 ] && [ $VAR -le 20 ]
1 Like

I agree with Yoda here. :slight_smile:

You can also use,

if [ $VAR -gt 10 -a $VAR -le 20 ]
1 Like

Hi joeyg,

thanks for the quick response and thank you for your observation, i've already modified that and what i got now is this:

{
 if [ $VAR -gt 11 ]
 then
    if [ $VAR -ge 17 ]
     then
        printf "\033[01;31m%s\033[00m\n" "$VAR"
     else
        printf "\033[01;33m%s\033[00m\n" "$VAR"
    fi
 else
        printf "\033[01;32m%s\033[00m\n" "$VAR"
 fi
}

the error is gone, and the scrip is working all right. thank you !!!

---------- Post updated at 10:07 AM ---------- Previous update was at 10:05 AM ----------

yes, i modified that too, thanks Yoda :b: