:integer expression expected

Hi,

echo $i

until [ "$NUM" -gt "0" ] || [ "$NUM" -le "$i" ]
do
read NUM
      if [ "$NUM" -lt "1" ] && [ "$NUM" -gt "$i" ] ; then
      printf "$FBOLD\nInvalid number, please enter valid backup number: $FREG"
      fi
done

Getting below error :

./import_location.sh: line 234: [: : integer expression expected
./import_location.sh: line 234: [: : integer expression expected
./import_location.sh: line 237: [: : integer expression expected
./import_location.sh: line 234: [: : integer expression expected
./import_location.sh: line 234: [: : integer expression expected

where mistake is done ?

With Regards

What value doesn $NUM have?

As the error says, it is expecting at

... [ "$NUM" ...

an integer.

Maybe echo $NUM to check what it currently is or add a set -x and a set +x to enable and disable debugging.

What Shell are you using?
What is a sample value for $i , and what does it signify?
What is a sample value for $NUM?
What is a valid range for $NUM ?

Which shell are you using ?
try to
double the bracket [[ ]]
initialize your variable
gather the contitions into one bracket [[ || ]] instead of [[ ]] || [[ ]]
remove the double quote inside condition brackets (should be the last thing to try)
... and see if a miracle happen :slight_smile:

what about this statement :

printf "$FBOLD\nInvalid number, please enter valid backup number: $FREG"

shouldn't it be smthg like

printf "%s \nInvalid number, please enter valid backup number:  " $FBOLD 
read FREG 

or whatever format should $FBOLD fit

I think your printf syntax may need to be checked

Try this:

echo $i

while true
do
    read NUM
    if [ $NUM -gt 0 -a $NUM -le $i ] 2> /dev/null; then
        break
    fi
    printf "$FBOLD\nInvalid number, please enter valid backup number: $FREG"
done

Also handles any non numeric NUM values: test will fail so break isn't executed, redirect stderr to /dev/null to hide shell error message.

1 Like