Home > front end >  Integer expression expected, SAP HANA stop script
Integer expression expected, SAP HANA stop script

Time:10-21

I'm currently building a bash script to stop our SAP HANA database and afterwards check if it suceeded in stopping it. It's a work in progress and also my first "longer" script I've written.

For some reason it tells me:

testscript.sh: line 19: [: : integer expression expected

why could this be? Please have a look at my script below

#### errocodes
#### 0  hanadb state = OK
#### 10 hanadb state = STOPPED
#### 20 hanadb state = WARNING
#### 30 hanadb state = ERROR

# stop HANA DB
sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop

# check hana db state
echo "Checking if Service is active"
HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`
if [ "$HANADBSTATUS" = GRAY ]
then
    echo "Hana DB is stopped."
    exit 10
else
    sleep 5
    while [ "$HANADBSTATUS" != GRAY ] && [ "$i" -lt 4 ]
        do
            echo "Warning: There seems to be a problem with stopping the HANA DB. Will retry 3-Times until Script will abort. This is retry Number: $i" 
            sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function Stop
            sleep 20
            HANADBSTATUS=`sudo /usr/sap/hostctrl/exe/sapcontrol -nr 00 -function GetProcessList`
            ((i  ))
            if [ "$i" = 3 ]
                then
                    echo "Error: retried 3-Times. Couldn't stop DB. Exiting Script."
                    echo "Script aborts with Error 0"
                    exit 0
            fi
        done
    fi
            

CodePudding user response:

Add set -x (enable debug mode) after sudo ..., run the script, and review the debug output.

My guess is this - [ "$i" -lt 4 ] - is generating the error because the variable i is (initially) undefined so the test becomes [ "" -lt 4 ] which generates the same error you're seeing:

$ typeset -p i
-bash: typeset: i: not found

$ if [ "$i" -lt 4 ]; then echo yes;fi
-bash: [: : integer expression expected

Enabling debug mode to show what the test looks like with an undefined i:

$ set -x

$ if [ "$i" -lt 4 ]; then echo yes;fi
  '[' '' -lt 4 ']'
-bash: [: : integer expression expected

One quick fix would be to insure i is initialized prior to the while loop, eg:

sleep 5
i=0

while [ "$HANADBSTATUS" != GRAY ] && [ "$i" -lt 4 ]
do
    ....
done
  • Related