if elif control issue

The below script is been ran on MYSQL database.... but control is not reaching to MYSQL elif block :wall:

The complete code is:

#!/usr/bin/ksh
 
MR=eml_msp131233
 
start=`date +%s%N`
 
echo "***Clean-up for Static Tables removal***" | tee  /tmp/$MR.log
 
        set -A mname1 "'"XYZ_100"'"        
 
echo "DB_SYSTEM =$DB_SYSTEM " | tee -a /tmp/$MR.log
if [ $DB_SYSTEM = "ORACLE" ] 
then
    echo "Deleting the entry table" | tee -a /tmp/$MR.log
        sqlplus -s $OPMDB_USR/$OSMDB_PWD <<!
        delete from xyz where modelname IN (${mname1[@]}); 
        commit;                
    exit
        !    
        echo "**** Required Model Names XYZ Deleted****" | tee -a /tmp/$MR.log
    i=0
    len=0
    len=${#port_add_tab
[*]}
    while [ $i -lt $len ]
    do         
               sqlplus -s $OPMDB_USR/$OSMDB_PWD << !
               drop table ${port_add_tab}; 
               exit
        !
       echo "****Table: ${port_add_tab} is deleted****" | tee -a /tmp/$MR.log
       i=`expr $i + 1`
    done
elif [ $DB_SYSTEM = "MYSQL" ] 
then    
    echo "Deleting the entry in Tables" | tee -a /tmp/$MR.log
        mysql --user=${OPMDB_USR} --password=${OPMDB_PWD} ${DB_NAME} << ! |tee -a /tmp/$MR.log       
        set storage_engine=INNODB;
        delete from XYZ where modelname IN (${mname1[@]}); 
        commit;             
    exit
!
        echo "**** Required Model XYZ Deleted****" | tee -a /tmp/$MR.log
    i=0
    len=0
    len=${#port_add_tab
[*]}
    while [ $i -lt $len ]
        do    
           mysql --user=${OPMDB_USR} --password=${OPMDB_PWD} ${DB_NAME} << ! | tee -a /tmp/$MR.log       
           set storage_engine=INPODB;
           drop table if exists ${port_add_tab}; 
           exit
!
           echo "****Table: ${port_add_tab} is deleted****" | tee -a /tmp/$MR.log
       i=`expr $i + 1`
       done
fi
 
end=`date +%s%N`
dif=$(($end - $start))
 
echo "Execution time in : $dif" | tee -a /tmp/$MR.log
echo "Done!! Script Executed successfully" | tee -a /tmp/$MR.log

Please help me....
please see the exe below:

+ MR=eml_msp131233
+ + date +%s%N
start=132367757620
+ echo ***This script is used for Migration Clean-up for Static Tables removal***
+ tee /tmp/eml_msp131233.log
***This script is used for DB Migration Clean-up for Static Tables removal***
+ set -A mname1 'XYZ_100'
+ echo DB_SYSTEM =MYSQL 
+ tee -a /tmp/eml_msp131233.log
DB_SYSTEM =MYSQL 
+ [ MYSQL = ORACLE ]
+ + date +%s%N
end=132367757620
+ dif=0
+ echo Execution time in : 0
+ tee -a /tmp/eml_msp131233.log
Execution time in : 0
+ echo Done!! Script Executed successfully
+ tee -a /tmp/eml_msp131233.log
Done!! Script Executed successfully
+ echo Please check the log in /tmp/eml_msp131233.log

It is reaching the block:

+ [ MYSQL = ORACLE ]

but it does not evaluate to true. In other words, "MYSQL" is not "ORACLE".

Check where and how the variable $DB_SYSTEM is set. It seems not in this script.

That is right zaxxon... MYSQL is not ORACLE...but then it should come to elif part it is not reaching there... that is the only problem:wall:

Seeking for further response....

Thanks, Ambar

You did not understand. Your code is

elif [ $DB_SYSTEM = "MYSQL" ] 

The debugging output of this test is

+ [ MYSQL = ORACLE ]

So everything is going right. It only enters the elif block if the condition is true, which is not the case and so skips the elif block. If this is really a MYSQL system, then your variable seems to have a wrong value, wherever it is set.

The above is the output of the first test. i.e if [ $DB_SYSTEM = "ORACLE" ]
Even if the value of $DB_SYSTEM is set wrongly, the shell should evaluate (true/false) the elif block just like first "if" which is not happening in this case.

Something seems to be wrong.
OP also echo'ing the value of $DB_SYSTEM which seems to be interpreted correctly with the below line.

+ echo DB_SYSTEM =MYSQL 
+ tee -a /tmp/eml_msp131233.log
DB_SYSTEM =MYSQL 

However, the value seems to have trailing white space, the test should ignore that because there is no quotes in the variable at the time of evaluation.

Sorry if I did not understand zaxxon's explanation.

You are right, sorry, my fault. Not sure what I read there.

Thanks zaxxon for your time; and also Thanks to make me understand the debug pattern. I have some how corrected my code :D.

Afterwards I realize there was issue with formatting/indentation also in the code...

if [ $DB_SYSTEM = "ORACLE" ]

i.e.

if [ $DB_SYSTEM="ORACLE" ]

I request if possible... can you please built some logical understanding for me in UNIX shell script indentation ...

Thanks again!!
Ambar Agrawal

---------- Post updated at 04:18 PM ---------- Previous update was at 04:11 PM ----------

Thanks Anchal, Yes problem seems with indentation in the script --

If []

Condition.

---------- Post updated at 04:38 PM ---------- Previous update was at 04:18 PM ----------

The below code ran fine now... Though I have given spaces between the '=' sign in the "IF Condition":

#!/usr/bin/ksh
MR=eml_msp131233
 
start=`date +%s%N`
i=0
len=0
 
echo "***Clean-up for Static Tables removal***" | tee  /tmp/$MR.log
 
        set -A mname1 "'"XYZ_100"'"        
echo "DB_SYSTEM =$DB_SYSTEM " | tee -a /tmp/$MR.log
 
if [ $DB_SYSTEM = "ORACLE" ]
then
    sqlplus -s $OSMDB_USR/$OSMDB_PWD<<!
     delete from pqr where modelname IN (${mname1[@]}); 
        commit;          
    exit
!
   echo "**** MY SQL Required Model Names Deleted****" | tee -a /tmp/$MR.log    
    len=${#port_add_tab
[*]}
    while [ $i -lt $len ]
        do    
           mysql --user=${OPMDB_USR} --password=${OPMDB_PWD} ${DB_NAME} << ! | tee -a /tmp/$MR.log       
           set storage_engine=IPNODB;
           drop table if exists ${port_add_tab}; 
           exit
!
           echo "****Table: ${port_add_tab} is dropped****" | tee -a /tmp/$MR.log
       i=`expr $i + 1`
       done
elif [ $DB_SYSTEM = "MYSQL" ]
then
    echo "hiiiiiii" | tee -a /tmp/$MR.log
    echo "Deleting the entry in  table" | tee -a /tmp/$MR.log
        mysql --user=${OSMDB_USR} --password=${OSMDB_PWD} ${DB_NAME} << ! |tee -a /tmp/$MR.log       
        set storage_engine=INNODB;
        delete from XYZ where modelname IN (${mname1[@]}); 
        commit;             
    exit
!
        echo "**** Required Model Names from Deleted****" | tee -a /tmp/$MR.log    
    len=${#port_add_tab
[*]}
    while [ $i -lt $len ]
        do    
           mysql --user=${OPMDB_USR} --password=${OPMDB_PWD} ${DB_NAME} << ! | tee -a /tmp/$MR.log       
           set storage_engine=IPNODB;
           drop table if exists ${port_add_tab}; 
           exit
!
           echo "****Table: ${port_add_tab} is dropped****" | tee -a /tmp/$MR.log
       i=`expr $i + 1`
       done
fi
 
end=`date +%s%N`
dif=$(($end - $start))
 
echo "Execution time in nano seconds : $dif" | tee -a /tmp/$MR.log
echo "Done!! Script Executed successfully" | tee -a /tmp/$MR.log
echo "Please check the log in /tmp/$MR.log"

Can some one put some light on this to built my understanding more clear...

---------- Post updated at 04:39 PM ---------- Previous update was at 04:38 PM ----------

Script Output is:

***This script is used for DB Migration Clean-up for Static Tables removal***
DB_SYSTEM =MYSQL 
hiiiiiii
MY SQL Deleting the entry in static tables and pa_tab_info table
**** MY SQL Required Model Names  Deleted****
****Table: XYZ_100C is dropped****
Execution time in : 100
Done!! Script Executed successfully
Please check the log in /tmp/eml_msp131233.log

Problem was the two lines like this:

        !

Because they were not in column 1, all the lines from the <<! to the next line which did properly start with ! were treated as part of the Here Document. Thus most of your code did not execute (and that includes the "elif" line).

Thanks a lot methyl... Now I completely understood!:slight_smile:

Thanks a ton!!!
Ambar Agrawal

You need spaces around comparison operators - your second example is equivalent to just if [ sometext ] (which is always true).

(Unlike assignment, where you don't want spaces!)