breaking out of while loop


 
Thread Tools Search this Thread
Operating Systems Linux breaking out of while loop
# 1  
Old 06-12-2008
breaking out of while loop

Hi ,
I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other threads too but still not clear .

db2 -x "select tabname from syscat.tables where tabschema='${source_schema}' and type='A'" > table_alias.lst
cat table_alias.lst | \
while read TN
do

typeset -l TN


base_table=`db2 -x "select base_tabname from syscat.tables where tabschema='${source_schema}' and tabname=upper('$TN') " `
echo ${base_table}
if [ ${base_table} != "DIM_$TN" || ${base_table} != "FACT_$TN" ] ; then
break
else
continue
fi

db2look -d ${dbname} -z ${source_schema} -t ${base_table} -e -x > VIEW.${source_schema}.$TN.${ecmdate}.sql
tr 'a-z' 'A-Z' < VIEW.${source_schema}.$TN.${ecmdate}.sql > $tmpfile_cln
cat $tmpfile_cln > VIEW.${source_schema}.$TN.${ecmdate}.sql
cat VIEW.${source_schema}.$TN.${ecmdate}.sql

Here is the sh -x part of it .

#sh -x alias_creation.shl uhcdev01 opsdm001 opsdm002
+ [[ -n 1 ]]
+ return
+ USAGE=USAGE: alias_creation.shl [Source_DBNAME-required] [Source_Schema] [Target_Schema]
+ [[ 3 -lt 3 ]]
+ dbname=uhcdev01
+ source_schema=opsdm001
+ target_schema=opsdm002
+ typeset -u dbname
+ tmpfile_tab=/tmp/tmpfile_tab
+ tmpfile_dep=/tmp/tmpfile_tab_dep
+ tmpfile_cln=/tmp/tmpfile_cln
+ tmpfile_cln1=/tmp/tmpfile_cln1
+ + date +%Y%m%d
ecmdate=20080612
+ db2 connect to UHCDEV01

Database Connection Information

Database server = DB2/AIX64 9.1.2
SQL authorization ID = DRAM
Local database alias = UHCDEV01

+ [ opsdm001 ]
+ source_schema=opsdm001
+ [ opsdm002 ]
+ target_schema=opsdm002
+ typeset -u source_schema
+ typeset -u target_schema
+ cat table_alias.lst
+ read TN
+ typeset -l TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('unique_provider_targ')
base_table=
+ echo

+ [ != DIM_unique_provider_targ
alias_creation.shl[65]: test: ] missing
+ != FACT_unique_provider_targ ]
alias_creation.shl[65]: !=: not found
+ continue
+ read TN
+ typeset -l TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('unique_provider_uhg_resp')
base_table=
+ echo

+ [ != DIM_unique_provider_uhg_resp
alias_creation.shl[65]: test: ] missing
+ != FACT_unique_provider_uhg_resp ]
alias_creation.shl[65]: !=: not found
+ continue
+ read TN
+ typeset -l TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('zip')
base_table=VW_ZIP
+ echo VW_ZIP
VW_ZIP
+ [ VW_ZIP != DIM_zip
alias_creation.shl[65]: test: ] missing
+ VW_ZIP != FACT_zip ]
alias_creation.shl[65]: VW_ZIP: not found
+ continue
+ read TN
+ typeset -l TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('zip_customer_segment')
base_table=VW_ZIP_CUSTOMER_SEGMENT
+ echo VW_ZIP_CUSTOMER_SEGMENT
VW_ZIP_CUSTOMER_SEGMENT
+ [ VW_ZIP_CUSTOMER_SEGMENT != DIM_zip_customer_segment
alias_creation.shl[65]: test: ] missing
+ VW_ZIP_CUSTOMER_SEGMENT != FACT_zip_customer_segment ]
alias_creation.shl[65]: VW_ZIP_CUSTOMER_SEGMENT: not found
+ continue
+ read TN
+ typeset -l TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('zip_provider_srvc')
base_table=VW_ZIP_PROVIDER_SRVC
+ echo VW_ZIP_PROVIDER_SRVC
VW_ZIP_PROVIDER_SRVC
+ [ VW_ZIP_PROVIDER_SRVC != DIM_zip_provider_srvc
alias_creation.shl[65]: test: ] missing
+ VW_ZIP_PROVIDER_SRVC != FACT_zip_provider_srvc ]
alias_creation.shl[65]: VW_ZIP_PROVIDER_SRVC: not found
+ continue
+ read TN
+ db2 terminate

Thanks in advance
# 2  
Old 06-15-2008
Break is used to break out of a loop, while continue is used to skip over a
step if it matches (fails to match) a condition.

Change this:
Code:
while read TN
do

typeset -l TN 

base_table=`db2 -x "select base_tabname from syscat.tables where
tabschema='${source_schema}' and tabname=upper('$TN') " `
echo ${base_table}
if [ ${base_table} != "DIM_$TN" || ${base_table} != "FACT_$TN" ] ; then
break 
else 
continue 
fi

to this:
Code:
while read TN
do
   if [ ${base_table} != "DIM_$TN" || ${base_table} != "FACT_$TN" ] ; then
      continue
   else
      typeset -l TN 

      base_table=`db2 -x "select base_tabname from syscat.tables where
      tabschema='${source_schema}' and tabname=upper('$TN') " `
      echo ${base_table}

   fi

That should cause the script to skip to the next iteration of the while loop if
either of the first conditions is true, else execute the else part of the if -
else - fi test.
# 3  
Old 06-16-2008
thanks a lot . This is working fine now Smilie
# 4  
Old 06-19-2008
My bad . i was in bit haste to reply earlier . I had to test it in multiple scripts .

The newer script is giving weird result .

The comparison part is failing and db2look is skipping. After the comparison , the script should go for db2look but it is continuing to next iteration . Here is sh -x part when I ran the script .

#sh -x alias_creation.shl uhcetl01 opsdm001 opsdm002
+ [[ -n 1 ]]
+ return
+ USAGE=USAGE: alias_creation.shl [Source_DBNAME-required] [Source_Schema] [Target_Schema]
+ [[ 3 -lt 3 ]]
+ dbname=uhcetl01
+ source_schema=opsdm001
+ target_schema=opsdm002
+ typeset -u dbname
+ tmpfile_tab=/tmp/tmpfile_tab
+ tmpfile_dep=/tmp/tmpfile_tab_dep
+ tmpfile_cln=/tmp/tmpfile_cln
+ tmpfile_cln1=/tmp/tmpfile_cln1
+ + date +%Y%m%d
ecmdate=20080619
+ db2 connect to UHCETL01

Database Connection Information

Database server = DB2/AIX64 9.1.2
SQL authorization ID = DRAM
Local database alias = UHCETL01

+ [ opsdm001 ]
+ source_schema=opsdm001
+ [ opsdm002 ]
+ target_schema=opsdm002
+ typeset -u source_schema
+ typeset -u target_schema
+ db2 -x select tabname from syscat.tables where tabschema='OPSDM001' and type='A'
+ 1> table_alias.lst
+ cat table_alias.lst
+ read TN
+ typeset -u TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('CLNOPS_CUSTOMER_SEGMENT')
base_table=DIM_CLNOPS_CUSTOMER_SEGMENT
+ echo DIM_CLNOPS_CUSTOMER_SEGMENT
DIM_CLNOPS_CUSTOMER_SEGMENT
+ [[ DIM_CLNOPS_CUSTOMER_SEGMENT != DIM_CLNOPS_CUSTOMER_SEGMENT ]]
+ echo No match found
No match found
+ echo ###################################
###################################
+ continue
+ read TN
+ typeset -u TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('CUSTOMER_SEGMENT')
base_table=DIM_CUSTOMER_SEGMENT
+ echo DIM_CUSTOMER_SEGMENT
DIM_CUSTOMER_SEGMENT
+ [[ DIM_CUSTOMER_SEGMENT != DIM_CUSTOMER_SEGMENT ]]
+ echo No match found
No match found
+ echo ###################################
###################################
+ continue
+ read TN
+ typeset -u TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('DISCHARGE_STATUS_CODE')
base_table=DIM_DISCHARGE_STATUS_CODE
+ echo DIM_DISCHARGE_STATUS_CODE
DIM_DISCHARGE_STATUS_CODE
+ [[ DIM_DISCHARGE_STATUS_CODE != DIM_DISCHARGE_STATUS_CODE ]]
+ echo No match found
No match found
+ echo ###################################
###################################
+ continue
+ read TN
+ typeset -u TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('FEE_SCHEDULE_CODE')
base_table=DIM_FEE_SCHEDULE_CODE
+ echo DIM_FEE_SCHEDULE_CODE
DIM_FEE_SCHEDULE_CODE
+ [[ DIM_FEE_SCHEDULE_CODE != DIM_FEE_SCHEDULE_CODE ]]
+ echo No match found
No match found
+ echo ###################################
###################################
+ continue
+ read TN
+ typeset -u TN
+ + db2 -x select base_tabname from syscat.tables where tabschema='OPSDM001' and tabname=upper('GENERIC_THERAPEUTIC_CLASS')
base_table=DIM_GENERIC_THERAPEUTIC_CLASS
+ echo DIM_GENERIC_THERAPEUTIC_CLASS
DIM_GENERIC_THERAPEUTIC_CLASS
+ [[ DIM_GENERIC_THERAPEUTIC_CLASS != DIM_GENERIC_THERAPEUTIC_CLASS ]]
+ echo No match found
No match found
+ echo ###################################
###################################
+ continue
+ read TN
+ db2 terminate
DB20000I The TERMINATE command completed successfully.

While my script code is as below


db2 -x "select tabname from syscat.tables where tabschema='${source_schema}' and type='A'" > table_alias.lst
cat table_alias.lst | \
while read TN
do

typeset -u TN


#################################################################################################### #########################################################
# Extracting DDL for base table and its dependent dependent objects along with their grants.
#################################################################################################### #########################################################

base_table=`db2 -x "select base_tabname from syscat.tables where tabschema='${source_schema}' and tabname=upper('$TN') " `
echo ${base_table}

if [[ "${base_table}" != "DIM_$TN" || "${base_table}" != "FACT_$TN" ]] ; then
echo " No match found "
echo " ################################### "
continue
else
typeset -l TN
echo " hi "
echo ${base_table}
fi

db2look -d ${dbname} -z ${source_schema} -t ${base_table} -e -x > VIEW.${source_schema}.$TN.${ecmdate}.sql
tr 'a-z' 'A-Z' < VIEW.${source_schema}.$TN.${ecmdate}.sql > $tmpfile_cln
cat $tmpfile_cln > VIEW.${source_schema}.$TN.${ecmdate}.sql
cat VIEW.${source_schema}.$TN.${ecmdate}.sql

sed -n '/^CREATE TABLE/,/COMPRESS/p' VIEW.${source_schema}.$TN.${ecmdate}.sql > $tmpfile_cln

cat $tmpfile_cln | grep -v "CREATE" | grep -v "COMPRESS" | awk '{ print $1, "," }' > VIEW.${source_schema}.$TN.${ecmdate}.sql
sed -e '$s/,/)/' VIEW.${source_schema}.$TN.${ecmdate}.sql > $tmpfile_cln
cat $tmpfile_cln > VIEW.${source_schema}.$TN.${ecmdate}.sql

#################################################################################################### ########################################################
# Cleaning the table
#################################################################################################### #########################################################

cat VIEW.${source_schema}.$TN.${ecmdate}.sql |egrep -v "^--|^$|CONNECT|COMMIT|TERMINATE" > $tmpfile_cln
cat $tmpfile_cln |egrep -v "SET CURRENT" > VIEW.${source_schema}.$TN.${ecmdate}.sql
echo "CONNECT TO $dbname ;" > $tmpfile_cln
echo "SET SESSION_USER $source_schema ;" >> $tmpfile_cln
echo "SET CURRENT SCHEMA $source_schema ; " >> $tmpfile_cln
echo " CREATE VIEW $source_schema.vw_$TN ( " >> $tmpfile_cln
cat VIEW.${source_schema}.$TN.${ecmdate}.sql >> $tmpfile_cln
echo " as select " >> $tmpfile_cln
sed -e '$s/)/ /' VIEW.${source_schema}.$TN.${ecmdate}.sql > $tmpfile_cln1
cat $tmpfile_cln1 >> $tmpfile_cln
echo "FROM $source_schema.$base_table " >> $tmpfile_cln
echo " ; " >> $tmpfile_cln
echo " DROP ALIAS $source_schema.$TN ; " >> $tmpfile_cln
echo " CREATE ALIAS $source_schema.$TN for vw_$TN ; " >> $tmpfile_cln

if [ ${source_schema} = 'OPSDM001' ]
then
echo " GRANT SELECT ON TABLE $source_schema.$TN TO group uhcdmtst , group dsdbdev,group selcoe01 ,group closel01 ,group closel02 ,group closel04 ,group closel05 ;" >> $tmpfile_cln
elif [ ${source_schema} = 'CLODM001' ]
then
echo "GRANT SELECT ON TABLE $source_schema.$TN TO group uhcdmtst,group dsdbdev,group selcoe01 ,group closel01 ,group closel02 ,group closel04 ,group closel05 ;" >> $tmpfile_cln
elif [ ${source_schema} = 'UHCDM001' ]
then
echo "GRANT SELECT ON TABLE $source_schema.$TN TO group UHCDMTST,group UHCDMSEL,group UHCDMSE2,group UHCDMSE3,group UHCDMSE4,group UHCDMSE5,group UHCDMSE6,group UHCDMSE7,group UHCDMSE8,group UHCDMSE9,group UHCDMS10,group UHCDMS11,group UHCDMS12,group UHCDMS13,group UHCDMS14 ;" >>$tmpfile_cln
else
" "
fi
echo "COMMIT WORK ; " >> $tmpfile_cln
echo "CONNECT RESET ; " >> $tmpfile_cln
echo "TERMINATE ; " >> $tmpfile_cln
sed -e 's/"/ /g' $tmpfile_cln > VIEW.${source_schema}.$TN.${ecmdate}.sql

#cp VIEW.${source_schema}.$TN.${ecmdate}.sql /dba_dir/uhcetl01/ddl/schema02/view
#################################################################################################### ########################################
# Cleaning up old files
#################################################################################################### #########################################################

rm ${tmpfile_tab}_$TN
rm ${tmpfile_dep}_$TN
rm $tmpfile_cln
# rm VIEW.${source_schema}.$TN.${ecmdate}.sql
done

db2 terminate


Any help is appreciated ,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

While loop breaking when using "ssh" command inside

Hi , I am trying to read a list of hosts from a config file and trying to get file list from that host. For this I have used one while loop. cat "$ARCHIVE_CFG_FILE" | sed '/^$/d' | sed '/^#/d' | while read ARCHIVE_CFG do SOURCE_SERVER_NAME=`echo "$ARCHIVE_CFG" | awk -F '|' '{ print... (2 Replies)
Discussion started by: Anupam_Halder
2 Replies

2. Shell Programming and Scripting

Breaking a pipe

Here's my code - for File in "${Files}"; do uuencode "${File}" "$(basename ${File} 2>&-)" 2>&-; done | mailx -s "subject" "a@b.c" Now I want to know if there a way to *not* send an email if the "Files" variables turns out to be blank. Any suggestions? Also, just to clarify, I... (4 Replies)
Discussion started by: nexional
4 Replies

3. Shell Programming and Scripting

Ssh to remote server loop is breaking

hi All, cat login.list server1 userid1 server2 userid2 server3 userid3 ---------------------------------------- #SSHSCRIPT.ksh FILE=login.list while read vah vah_id do ssh $vah -l $vah_id "pwd" done < "$FILE" ----------------------------------------- When i... (2 Replies)
Discussion started by: raghur77
2 Replies

4. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

5. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

6. Shell Programming and Scripting

Breaking up a file

Hi, I have a file that looks like this - lets call it fileA >hhm2 IIIIIIIIILLLLLLLMMMMMMMMMNNNNNNNNNNGGGGGGHHHHHHHH >hhm4 OOOOOKKKKKKKKMMMMMHHHHHLLLLLLLLWWWWWWWWWWW >hhm9 OOOOOOOIIIIIIIIIKKKKKKKKKMMMMMHHHHHHHHHHHLLLLLLLLLL So the file is pretty straight forward. The name is indicated... (2 Replies)
Discussion started by: phil_heath
2 Replies

7. Shell Programming and Scripting

Breaking if-else loop and variety of comparisions

Hello Friends, Im trying to write a script to invoke nagios. In order to do this I grep some words that comes from output of some backup scripts. When there is "End-of-tape detected" in directed output logs it should give alarm. First I would like to know if there is any better way to write... (5 Replies)
Discussion started by: EAGL€
5 Replies

8. Shell Programming and Scripting

Breaking Loop by using another script

Hi friends, I have 2 scripts. 1) Master_Script.sh and 2) Sub_script.sh We run Master_script.sh manually where as sub_script.sh keeps generating output in every 2 minutes (through crontab). The output generated by sub_script.sh can be 0 or 1. As I told you, sub-script.sh keeps generating o/p... (7 Replies)
Discussion started by: anushree.a
7 Replies

9. Shell Programming and Scripting

rsh breaking me out of loop

Hey all I have two scripts, one script containing the guts of my code. The other simply loops through a list, calling the other script on each iteration. Problem is when I add the line `/usr/bin/rsh -l root $HOSTNAME ""` to my main script, the loop never seems to exectute any more... (1 Reply)
Discussion started by: mark007
1 Replies

10. HP-UX

Breaking Mirror

Can some one point this UNIX newbie to a web site or directions on the steps needed to break a mirror in HP-UNIX to change a bad hard drive. (4 Replies)
Discussion started by: egress1
4 Replies
Login or Register to Ask a Question