if elif control issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting if elif control issue
# 1  
Old 12-12-2011
if elif control issue

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

The complete code is:
Code:
#!/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[i]}; 
               exit
        !
       echo "****Table: ${port_add_tab[i]} 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[i]}; 
           exit
!
           echo "****Table: ${port_add_tab[i]} 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:

Code:
+ 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


Moderator's Comments:
Mod Comment Please use code tags!

Last edited by ambarginni; 12-13-2011 at 01:47 AM.. Reason: company policy
# 2  
Old 12-12-2011
It is reaching the block:
Code:
+ [ 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.
# 3  
Old 12-12-2011
Script is been run on MYSQL database

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 problemSmilie

Seeking for further response....

Thanks, Ambar
# 4  
Old 12-12-2011
You did not understand. Your code is
Code:
elif [ $DB_SYSTEM = "MYSQL" ]

The debugging output of this test is
Code:
+ [ 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.
# 5  
Old 12-12-2011
Quote:
Code:
+ [ MYSQL = ORACLE ]

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.

Code:
+ 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.
This User Gave Thanks to clx For This Post:
# 6  
Old 12-12-2011
You are right, sorry, my fault. Not sure what I read there.
# 7  
Old 12-12-2011
Thanks zaxxon

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

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

Code:
if [ $DB_SYSTEM = "ORACLE" ]

i.e.
Code:
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 --
Code:
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":

Code:
#!/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[i]}; 
           exit
!
           echo "****Table: ${port_add_tab[i]} 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[i]}; 
           exit
!
           echo "****Table: ${port_add_tab[i]} 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:

Code:
***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


Last edited by ambarginni; 12-22-2011 at 06:52 AM.. Reason: company policy
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with condition "if then elif else fi"

Hi everybody, I must get trought a shell script 3 arguments. 1st argument = $1 (can take values REP1..4) 2nd argument = $2 (can take values A..Z) 3rd arguement = $3 (also can take values A...Z) I've written this code : #!/bin/bash if then liste=/data/folder1 echo... (6 Replies)
Discussion started by: shellX
6 Replies

2. Shell Programming and Scripting

If / elif

Hello all, I have a scenario where I take user input values and accordingly take actions say I run a script with sh scriptname -x GB -e txt (txt can also be text) I have used if clause for the first input (-x GB)and it is working fine Now for second the scenario is if then echo... (3 Replies)
Discussion started by: nnani
3 Replies

3. UNIX for Advanced & Expert Users

if else elif

Hi all,i have configured the following script to check if the file exists or not, #!/bin/sh sleep 30 { FILEFULL=$1`date +$2`* if ; then echo $FILEFULL exist else echo "$FILEFULL File not Found" | mail -s 'server' myaccount@mydomain.com fi } but i have a problem, i need to... (2 Replies)
Discussion started by: charli1
2 Replies

4. Homework & Coursework Questions

if/elif help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: This is my problem for the class. Write a script that asks for the user's age. If it is equal to or higher... (6 Replies)
Discussion started by: aggie6970
6 Replies

5. SuSE

Installin Suse 11 - Mouse issue using Remote Control

hi guys I want to exhaust all the possibilities so I'm going to ask this here I am installing a Suse 11 on IBM Blade Center using Remote Control. when using this is impossible to use the mouse I see the mouse but I have no control over it I try to move it but no way it moves to fast s no... (3 Replies)
Discussion started by: kopper
3 Replies

6. Shell Programming and Scripting

If -elif-else error.

I am getting below error from this code (which is at line 24): if ] #this is line24 in code then mv $File_source_path/$File_name $File_name'_'`date '+%d%m%y'` Error: line 24: Any help with the syntax. I am putting 2 condition with 'AND' clause. This is bash shell. (2 Replies)
Discussion started by: amit.mathur08
2 Replies

7. UNIX for Dummies Questions & Answers

Odd Control Character issue ^A

Sorry to bug you, but my sed is failing me, I have a file auto generated from abinitio, it has a string of chars ending with a line break, and then it has added a ^A character, I can remove this is vi by using the following %s/^A//g (where ^A is ctrl v and control A), however when I try to sed... (1 Reply)
Discussion started by: badg3r
1 Replies

8. Shell Programming and Scripting

If..elif..else...fi

Hi all, I got some problems on executing the following scripts. Scripts: if ]; then echo "M${str}O 0 1" >> ${tempFile} elif ]; then echo "M${str}O 1 0" >> ${tempFile} else echo "M${str}O 0 0" >> ${tempFile} fi Error: "`;' is not expected." what's the problem? (2 Replies)
Discussion started by: Rock
2 Replies

9. Shell Programming and Scripting

elif not expected

Hi, I have a shell script which runs fine when i do it from Unix simulator on Widnows. When i try to run the same for Unix it throws me the error syntax error at line 87: `elif' unexpected. Piece of code for the same is while do if ; then shift configFile="$1"... (9 Replies)
Discussion started by: pv0428
9 Replies
Login or Register to Ask a Question