error in if statement

Hi ,
I am getting an error when I run the script for checking word "view" in a file . I am using if statement. like this

if [ cat $TN.${ecmdate}.sql1 | grep -i view ]
then
VW_VAR=` cat $TN.${ecmdate}.sql1 | grep -i view | awk '{print $3}' | cut -d '.' -f2 `
echo " VW_$VW_VAR "
sed -e 's/'${VW_VAR}'/VW_'${VW_VAR}'/g' $TN.${ecmdate}.sql1 > $TN.${ecmdate}.sql
else
cat $TN.${ecmdate}.sql1 > $TN.${ecmdate}.sql
fi

But I am getting error in the end like this

migration_orig.shl[106]: test: ] missing
grep: can't open ]

the script name is migration_orig.shl .

Any input is appreciated .

It looks like you're using /bin/sh

try:

grep -iq 'view' $TN.${ecmdate}.sql1 
if [  $? -eq 0 ] 

I am pasting the code that I am using .
I am trying to get ddl based on table names passed in table.lst file .
I am doing 2 things here.
a) Changing tablespace name to _S02 . ( Thanks for the help on that )
b) Adding VW as prefix to all the view names .

I have written error part in the section where I get the error .

I get error on adding VW section.
It works fine for tablespace name add . But doesn't change the View name.
i.e. view name = DAYA.DIM_DATE
DESIRED OUTPUT I WANT IS LIKE THIS
DAYA.VW_DIM_DATE

#!/bin/ksh
USAGE='USAGE: miglook.shl [Source_DBNAME-required] [Source_Schema] [Target_Schema]'

if [[ $# -lt 3 ]]; then
clear
echo "\nIncorrect usage!!\n"
echo "Script was invoked last time as: miglook.shl $"
echo "\nThe correct usage is:\n"
echo "$USAGE"
echo "\n
********************************************************************************************************************************************"
echo "Arguments:"
echo "Source_DBNAME i.e. Name of database from which to extract DDL"
echo "Source_Schema i.e. Name of schema from which objects DDL need to be extracted"
echo "Target_Schema i.e. Name of schema for which DDL need to be created\n"
echo "*********************************************************************************************************************************************\n"
exit
fi

dbname=$1
source_schema=$2
target_schema=$3

typeset -u dbname

tmpfile_tab=/tmp/tmpfile_tab
tmpfile_dep=/tmp/tmpfile_tab_dep
tmpfile_cln=/tmp/tmpfile_cln

ecmdate=`date +%Y%m%d`
db2 connect to $dbname

if [ $2 ]
then
source_schema=$2
else
source_schema='UHCDM001'
fi

if [ $3 ]
then
target_schema=$3
else
target_schema=${source_schema}
fi

typeset -u source_schema
typeset -u target_schema
cat table.lst | \
while read TN
do

#############################################################################################################################################################
# convert the table name read to lowercase to statdardise for tkt name.
#############################################################################################################################################################

typeset -l TN

#############################################################################################################################################################
# Create list of dependent objects for base table
#############################################################################################################################################################

echo $TN > ${tmpfile_tab}$TN
db2 -x "select tabname from syscat.tabdep where bschema=upper('${source_schema}') and bname=upper('$TN') and btype in ('T','V','S') " > ${tmpfile_dep}
$TN

cat ${tmpfile_dep}$TN | while read line
do
db2 -x "select tabname from syscat.tabdep where bschema=upper('${source_schema}') and bname=upper('$line') and btype in ('T','V','S') " >> ${tmpfile_dep}
$TN
done

cat ${tmpfile_dep}$TN >> ${tmpfile_tab}$TN

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

cat ${tmpfile_tab}_$TN | while read tname
do
db2look -d ${dbname} -z ${source_schema} -t ${tname} -e -x >> $TN.${ecmdate}.sql
cat $TN.${ecmdate}.sql | tr a-z A-Z > $TN.${ecmdate}.sql

done

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

    cat $TN.$\{ecmdate\}.sql |egrep -v "^--|^$|CONNECT|COMMIT|TERMINATE" > $tmpfile_cln
    cat $tmpfile_cln |egrep -v "SET CURRENT" > $TN.$\{ecmdate\}.sql
    echo "CONNECT TO $dbname ;" > $tmpfile_cln
    echo "SET SESSION_USER $target_schema ;" >> $tmpfile_cln
    echo "SET CURRENT SCHEMA $target_schema ; " >> $tmpfile_cln
    cat $TN.$\{ecmdate\}.sql >> $tmpfile_cln
    echo "COMMIT WORK ; " >> $tmpfile_cln
    echo "CONNECT RESET ; " >> $tmpfile_cln
    echo "TERMINATE ; " >> $tmpfile_cln

#############################################################################################################################################################
# If moving same set of tables from one schema to other schema in same datebase then rename schema correctly
#############################################################################################################################################################

    if [[ $\{source_schema\} != $\{target_schema\} ]]; then
    cat $tmpfile_cln | sed -e 's/'$\{source\_schema\}'/'$\{target_schema\}'/g' > $TN.$\{ecmdate\}.sql

    typeset -l source_schema
    typeset -l target_schema
\#    cat $TN.$\{ecmdate\}.sql1 >  $TN.$\{ecmdate\}.sql 
    cat $TN.$\{ecmdate\}.sql | sed -e 's/'$\{source\_schema\}'/'$\{target_schema\}'/g' > $tmpfile_cln
    cat $TN.$\{ecmdate\}.sql > $tmpfile_cln
    sed -e 's/\\\(TEST[^"]*\\\)/\\1_S02/g' $tmpfile_cln  >  $TN.$\{ecmdate\}.sql

##############################################################################
#checks if the file has view in it ADD VW_ TO VIEW NAME (ERROR PART )
#############################################################################
grep -iq 'view' $TN.${ecmdate}.sql
if [ $? -eq 0 ]
then
VW_VAR=` cat $TN.${ecmdate}.sql | grep -i view | awk '{print $3}' | cut -d '.' -f2 `
echo " VW_$VW_VAR "
sed -e 's/'${VW_VAR}'/VW_'${VW_VAR}'/g' $TN.${ecmdate}.sql > $tmpfile_cln
else
cat $TN.${ecmdate}.sql > $tmpfile_cln
fi
$tmpfile_cln > $TN.${ecmdate}.sql

################################################################################################

    \#cat $TN.$\{ecmdate\}.sql 
    typeset -u source_schema
    typeset -u target_schema
    fi

############################################################################################################################################
# Cleaning up old files
#############################################################################################################################################################

    rm $\{tmpfile\_tab\}_$TN 
    rm $\{tmpfile\_dep\}_$TN
    rm $tmpfile_cln                                                         

done

db2 terminate

any help is appreciated .

Thanks a lot in advance !

I modified the add VW_ section. When I run it as sh -x scriptname <dbname> <source_schema> <target schema>
the output on screen is exactly what I want but the files generated is not the one. I am missing the output of files somewhere .

##############################################################################
#checks if the file has view in it
#############################################################################
cat $TN.${ecmdate}.sql | grep -i 'view'
if [ $? -eq 0 ]
then
VW_VAR=` cat $TN.${ecmdate}.sql | grep -i view | awk '{print $3}' | cut -d '.' -f2 `
echo " VW_$VW_VAR "
sed -e 's/'${VW_VAR}'/VW_'${VW_VAR}'/g' $TN.${ecmdate}.sql > $tmpfile_cln
cat $tmpfile_cln
# cat $tmpfile_cln > $TN.${ecmdate}.sql
else
cat $TN.${ecmdate}.sql > $tmpfile_cln
fi

################################################################################################

help please !

It would really help to see what the output is supposed to be, and what you are currently getting.

As an aside, you have multiple occurrences of an anti-pattern known as Useless Use of Cat, and also Useless Use of Grep and Useless Use of Test $?.

cat file | grep anything can always be rephrased as grep anything file as long as a single file is involved.

grep anything | awk '{ print $something }' can basically always be rephrased as awk '/anything/ { print $something }' (use the awk tolower() function on the search string to ignore case).

grep something; if [ $? -eq 0 ] ... can fruitfully be simplified to simply if grep something ...

This is not to pick on you (maybe pick a little bit on jim who ought to know better :slight_smile: -- rather an attempt at helping you make your scripts more idiomatic and readable.

if ( grep view dim_date.20080512.sql )
then
VW_VAR=` awk '/VIEW/ {print $3}' dim_date.20080512.sql | cut -d '.' -f2 `
echo " VW_${VW_VAR} "
sed -e 's/'${VW_VAR}'/VW_'${VW_VAR}'/g' dim_date.20080512.sql
cat $tmpfile_cln
else
cat dim_date.20080512.sql
fi

I am not getting the value for VW_VAR .

Please help here ......

You are grepping for lowercase view but in the awk script, you act on uppercase VIEW.

Anyway, solving the original problem is probably a more pressing issue at the moment; maybe then you can address stylistic issues in the scripts, and revert to a working version if a change turns out to break stuff. Sorry if I managed to lead you astray. (Having said that, the parentheses around the grep are redundant ...)

An example of the sh -x output as compared to what happens in reality would still be a good help.

Thanks again ERA !
As you mentioned I changed grep to upper case and it works.
Now , I am posting the script with sh -x .

#sh -x migration_orig.shl tst103 daya opsdm002
+ [[ -n 1 ]]
+ return
+ USAGE=USAGE: miglook.shl [Source_DBNAME-required] [Source_Schema] [Target_Schema]
+ [[ 3 -lt 3 ]]
+ dbname=tst103
+ source_schema=daya
+ target_schema=opsdm002
+ typeset -u dbname
+ tmpfile_tab=/tmp/tmpfile_tab
+ tmpfile_dep=/tmp/tmpfile_tab_dep
+ tmpfile_cln=/tmp/tmpfile_cln
+ + date +%Y%m%d
ecmdate=20080512
+ db2 connect to TST103

Database Connection Information

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

+ [ daya ]
+ source_schema=daya
+ [ opsdm002 ]
+ target_schema=opsdm002
+ typeset -u source_schema
+ typeset -u target_schema
+ cat table.lst
+ read TN
+ typeset -l TN
+ echo dim_date
+ 1> /tmp/tmpfile_tab_dim_date
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('dim_date') and btype in ('T','V','S')
+ 1> /tmp/tmpfile_tab_dep_dim_date
+ cat /tmp/tmpfile_tab_dep_dim_date
+ read line
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('DIM_DATE_VIEW') and btype in ('T','V','S')
+ 1>> /tmp/tmpfile_tab_dep_dim_date
+ read line
+ cat /tmp/tmpfile_tab_dep_dim_date
+ 1>> /tmp/tmpfile_tab_dim_date
+ cat /tmp/tmpfile_tab_dim_date
+ read tname
+ db2look -d TST103 -z DAYA -t dim_date -e -x
+ 1>> dim_date.20080512.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_date.20080512.sql
+ tr a-z A-Z
+ 1> dim_date.20080512.sql
+ read tname
+ db2look -d TST103 -z DAYA -t DIM_DATE_VIEW -e -x
+ 1>> dim_date.20080512.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_date.20080512.sql
+ tr a-z A-Z
+ 1> dim_date.20080512.sql
+ read tname
+ cat dim_date.20080512.sql
+ egrep -v ^--|^$|CONNECT|COMMIT|TERMINATE
+ 1> /tmp/tmpfile_cln
+ cat /tmp/tmpfile_cln
+ egrep -v SET CURRENT
+ 1> dim_date.20080512.sql
+ echo CONNECT TO TST103 ;
+ 1> /tmp/tmpfile_cln
+ echo SET SESSION_USER OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ echo SET CURRENT SCHEMA OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ cat dim_date.20080512.sql
+ 1>> /tmp/tmpfile_cln
+ echo COMMIT WORK ;
+ 1>> /tmp/tmpfile_cln
+ echo CONNECT RESET ;
+ 1>> /tmp/tmpfile_cln
+ echo TERMINATE ;
+ 1>> /tmp/tmpfile_cln
+ [[ DAYA != OPSDM002 ]]
+ cat /tmp/tmpfile_cln
+ sed -e s/DAYA/OPSDM002/g
+ 1> dim_date.20080512.sql
+ typeset -l source_schema
+ typeset -l target_schema
+ cat dim_date.20080512.sql
+ sed -e s/daya/opsdm002/g
+ 1> /tmp/tmpfile_cln
+ cat dim_date.20080512.sql
+ 1> /tmp/tmpfile_cln
+ sed -e s/\(TEST[^"]*\)/\1_S02/g /tmp/tmpfile_cln
+ 1> dim_date.20080512.sql
+ grep VIEW dim_date.20080512.sql
CREATE VIEW OPSDM002.DIM_DATE_VIEW AS SELECT DT_SYS_ID , FULL_DT FROM OPSDM002.DIM_DATE;
+ + awk /VIEW/ {print $3} dim_date.20080512.sql
+ cut -d . -f2
VW_VAR=DIM_DATE_VIEW
+ echo VW_DIM_DATE_VIEW
VW_DIM_DATE_VIEW
+ sed -e s/DIM_DATE_VIEW/VW_DIM_DATE_VIEW/g dim_date.20080512.sql
+ 1> /tmp/tmpfile_cln
+ cat /tmp/tmpfile_cln
CONNECT TO TST103 ;
SET SESSION_USER OPSDM002 ;
SET CURRENT SCHEMA OPSDM002 ;

CREATE TABLE "OPSDM002 "."DIM_DATE" (
"DT_SYS_ID" SMALLINT NOT NULL ,
"FULL_DT" DATE NOT NULL ,
"DAY_ABBR_CD" CHAR(3) NOT NULL ,
"MONTH_NM" CHAR(9) NOT NULL ,
"DAY_WK" SMALLINT NOT NULL ,
"WEEK_DAY_NM" CHAR(9) NOT NULL ,
"MONTH_NBR" SMALLINT NOT NULL ,
"LST_DAY_MO_IND" CHAR(1) NOT NULL ,
"DAY_NBR" SMALLINT NOT NULL ,
"MONTH_ABBR_CD" CHAR(3) NOT NULL ,
"QUARTER_NM" CHAR(6) NOT NULL ,
"QUARTER_NBR" SMALLINT NOT NULL ,
"YEAR_NBR" SMALLINT NOT NULL ,
"SAME_WEEK_DAY_YR_AGO_DT" DATE NOT NULL ,
"SEQ_NBR" SMALLINT NOT NULL ,
"WEEK_BEGN_DT" DATE NOT NULL ,
"WEEK_DAY_IND" CHAR(1) NOT NULL ,
"WEEK_NBR_YR" SMALLINT NOT NULL ,
"YEAR_MO" CHAR(6) NOT NULL ,
"LOAD_DT" DATE NOT NULL WITH DEFAULT CURRENT DATE ,
"UPDT_DT" DATE NOT NULL WITH DEFAULT CURRENT DATE )
COMPRESS YES
DISTRIBUTE BY HASH("DT_SYS_ID")
IN "TESTTBLSP_S02" ;
CREATE VIEW OPSDM002.VW_DIM_DATE_VIEW AS SELECT DT_SYS_ID , FULL_DT FROM OPSDM002.DIM_DATE;

GRANT ALTER ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT DELETE ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT INDEX ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT INSERT ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT SELECT ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT UPDATE ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
GRANT REFERENCES ON TABLE "OPSDM002 "."DIM_DATE" TO USER "DRAM " ;
COMMIT WORK ;
CONNECT RESET ;
TERMINATE ;
+ typeset -u source_schema
+ typeset -u target_schema
+ rm /tmp/tmpfile_tab_dim_date
+ rm /tmp/tmpfile_tab_dep_dim_date
+ rm /tmp/tmpfile_cln
+ read TN
+ typeset -l TN
+ echo dim_company
+ 1> /tmp/tmpfile_tab_dim_company
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('dim_company') and btype in ('T','V','S')
+ 1> /tmp/tmpfile_tab_dep_dim_company
+ cat /tmp/tmpfile_tab_dep_dim_company
+ read line
+ cat /tmp/tmpfile_tab_dep_dim_company
+ 1>> /tmp/tmpfile_tab_dim_company
+ cat /tmp/tmpfile_tab_dim_company
+ read tname
+ db2look -d TST103 -z DAYA -t dim_company -e -x
+ 1>> dim_company.20080512.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_company.20080512.sql
+ tr a-z A-Z
+ 1> dim_company.20080512.sql
+ read tname
+ cat dim_company.20080512.sql
+ egrep -v ^--|^$|CONNECT|COMMIT|TERMINATE
+ 1> /tmp/tmpfile_cln
+ cat /tmp/tmpfile_cln
+ egrep -v SET CURRENT
+ 1> dim_company.20080512.sql
+ echo CONNECT TO TST103 ;
+ 1> /tmp/tmpfile_cln
+ echo SET SESSION_USER OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ echo SET CURRENT SCHEMA OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ cat dim_company.20080512.sql
+ 1>> /tmp/tmpfile_cln
+ echo COMMIT WORK ;
+ 1>> /tmp/tmpfile_cln
+ echo CONNECT RESET ;
+ 1>> /tmp/tmpfile_cln
+ echo TERMINATE ;
+ 1>> /tmp/tmpfile_cln
+ [[ DAYA != OPSDM002 ]]
+ cat /tmp/tmpfile_cln
+ sed -e s/DAYA/OPSDM002/g
+ 1> dim_company.20080512.sql
+ typeset -l source_schema
+ typeset -l target_schema
+ cat dim_company.20080512.sql
+ sed -e s/daya/opsdm002/g
+ 1> /tmp/tmpfile_cln
+ cat dim_company.20080512.sql
+ 1> /tmp/tmpfile_cln
+ sed -e s/\(TEST[^"]*\)/\1_S02/g /tmp/tmpfile_cln
+ 1> dim_company.20080512.sql
+ grep VIEW dim_company.20080512.sql
+ cat dim_company.20080512.sql
+ 1> /tmp/tmpfile_cln
+ typeset -u source_schema
+ typeset -u target_schema
+ rm /tmp/tmpfile_tab_dim_company
+ rm /tmp/tmpfile_tab_dep_dim_company
+ rm /tmp/tmpfile_cln
+ read TN
+ db2 terminate
DB20000I The TERMINATE command completed successfully.
apsp8642:/dmartdev/dram/work #

If you notice , the cat file shows the VW_DIM_DATE_VIEW in the output with sh -x but the generated files are not the same . I have been trying but I am unable to get the desired output.
The output file has the following line

CREATE VIEW OPSDM002.DIM_DATE_VIEW AS SELECT DT_SYS_ID , FULL_DT FROM OPSDM002.DIM_DATE;

While I want the output like this

CREATE VIEW OPSDM002.VW_DIM_DATE_VIEW AS SELECT DT_SYS_ID , FULL_DT FROM OPSDM002.DIM_DATE;

Thanks,

Daya

I don't understand this part just after the sequence which you indicate was problematic in your original script:

It would appear that this is replacing $TN.${ecmdate}.sql with an empty file (and an error message, I would guess) before the new $tmpfile_cln is used for anything. Maybe you intended to "cat $tmpfile_cln > $TN.${ecmdate}.sql"? (But then why not do it the other way around inside the conditional in the first place.)

Thanks a lot ERA !!!!

It is working fine :slight_smile:

Hi Era ,

Once again , I am having issue if there are multiple occurances of word view in a file . when I ran the code with sh -x , I get the following messages .

sh -x migration_orig.shl tst103 daya opsdm002
+ [[ -n 1 ]]
+ return
+ USAGE=USAGE: migration_orig.shl [Source_DBNAME-required] [Source_Schema] [Target_Schema]
+ [[ 3 -lt 3 ]]
+ dbname=tst103
+ source_schema=daya
+ target_schema=opsdm002
+ typeset -u dbname
+ tmpfile_tab=/tmp/tmpfile_tab
+ tmpfile_dep=/tmp/tmpfile_tab_dep
+ tmpfile_cln=/tmp/tmpfile_cln
+ + date +%Y%m%d
ecmdate=20080513
+ db2 connect to TST103

Database Connection Information

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

+ [ daya ]
+ source_schema=daya
+ [ opsdm002 ]
+ target_schema=opsdm002
+ typeset -u source_schema
+ typeset -u target_schema
+ db2 -x select tabname from syscat.tables where tabschema='DAYA' and type='T'
+ 1> table.lst
+ cat table.lst
DIM_COMPANY
DIM_DATE
+ cat table.lst
+ read TN
+ typeset -l TN
+ echo dim_company
+ 1> /tmp/tmpfile_tab_dim_company
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('dim_company') and btype in ('T','V','S')
+ 1> /tmp/tmpfile_tab_dep_dim_company
+ cat /tmp/tmpfile_tab_dep_dim_company
+ read line
+ cat /tmp/tmpfile_tab_dep_dim_company
+ 1>> /tmp/tmpfile_tab_dim_company
+ cat /tmp/tmpfile_tab_dim_company
+ read tname
+ db2look -d TST103 -z DAYA -t dim_company -e -x
+ 1>> dim_company.20080513.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_company.20080513.sql
+ tr a-z A-Z
+ 1> dim_company.20080513.sql
+ read tname
+ cat dim_company.20080513.sql
+ egrep -v ^--|^$|CONNECT|COMMIT|TERMINATE
+ 1> /tmp/tmpfile_cln
+ cat /tmp/tmpfile_cln
+ egrep -v SET CURRENT
+ 1> dim_company.20080513.sql
+ echo CONNECT TO TST103 ;
+ 1> /tmp/tmpfile_cln
+ echo SET SESSION_USER OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ echo SET CURRENT SCHEMA OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ cat dim_company.20080513.sql
+ 1>> /tmp/tmpfile_cln
+ echo COMMIT WORK ;
+ 1>> /tmp/tmpfile_cln
+ echo CONNECT RESET ;
+ 1>> /tmp/tmpfile_cln
+ echo TERMINATE ;
+ 1>> /tmp/tmpfile_cln
+ [[ DAYA != OPSDM002 ]]
+ cat /tmp/tmpfile_cln
+ sed -e s/DAYA/OPSDM002/g
+ 1> dim_company.20080513.sql
+ typeset -l source_schema
+ typeset -l target_schema
+ cat dim_company.20080513.sql
+ sed -e s/daya/opsdm002/g
+ 1> /tmp/tmpfile_cln
+ cat dim_company.20080513.sql
+ 1> /tmp/tmpfile_cln
+ [ daya=DAYA ]
+ sed -e s/\(TEST[^"]*\)/\1_S02/g /tmp/tmpfile_cln
+ 1> dim_company.20080513.sql
+ grep VIEW dim_company.20080513.sql
+ echo file does not have view in it
file does not have view in it
+ cat dim_company.20080513.sql
+ 1> /tmp/tmpfile_cln
+ typeset -u source_schema
+ typeset -u target_schema
+ rm /tmp/tmpfile_tab_dim_company
+ rm /tmp/tmpfile_tab_dep_dim_company
+ rm /tmp/tmpfile_cln
+ read TN
+ typeset -l TN
+ echo dim_date
+ 1> /tmp/tmpfile_tab_dim_date
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('dim_date') and btype in ('T','V','S')
+ 1> /tmp/tmpfile_tab_dep_dim_date
+ cat /tmp/tmpfile_tab_dep_dim_date
+ read line
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('DIM_DATE_2') and btype in ('T','V','S')
+ 1>> /tmp/tmpfile_tab_dep_dim_date
+ read line
+ db2 -x select tabname from syscat.tabdep where bschema=upper('DAYA') and bname=upper('DIM_DATE_VIEW') and btype in ('T','V','S')
+ 1>> /tmp/tmpfile_tab_dep_dim_date
+ read line
+ cat /tmp/tmpfile_tab_dep_dim_date
+ 1>> /tmp/tmpfile_tab_dim_date
+ read tname
+ cat /tmp/tmpfile_tab_dim_date
+ db2look -d TST103 -z DAYA -t dim_date -e -x
+ 1>> dim_date.20080513.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_date.20080513.sql
+ tr a-z A-Z
+ 1> dim_date.20080513.sql
+ read tname
+ db2look -d TST103 -z DAYA -t DIM_DATE_2 -e -x
+ 1>> dim_date.20080513.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ cat dim_date.20080513.sql
+ tr a-z A-Z
+ 1> dim_date.20080513.sql
+ read tname
+ db2look -d TST103 -z DAYA -t DIM_DATE_VIEW -e -x
+ 1>> dim_date.20080513.sql
-- No userid was specified, db2look tries to use Environment variable USER
-- USER is: DRAM
-- Specified SCHEMA is: DAYA
-- The db2look utility will consider only the specified tables
-- Creating DDL for table(s)

-- Schema name is ignored for the Federated Section
+ tr a-z A-Z
+ cat dim_date.20080513.sql
+ 1> dim_date.20080513.sql
+ read tname
+ egrep -v ^--|^$|CONNECT|COMMIT|TERMINATE
+ cat dim_date.20080513.sql
+ 1> /tmp/tmpfile_cln
+ egrep -v SET CURRENT
+ cat /tmp/tmpfile_cln
+ 1> dim_date.20080513.sql
+ echo CONNECT TO TST103 ;
+ 1> /tmp/tmpfile_cln
+ echo SET SESSION_USER OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ echo SET CURRENT SCHEMA OPSDM002 ;
+ 1>> /tmp/tmpfile_cln
+ cat dim_date.20080513.sql
+ 1>> /tmp/tmpfile_cln
+ echo COMMIT WORK ;
+ 1>> /tmp/tmpfile_cln
+ echo CONNECT RESET ;
+ 1>> /tmp/tmpfile_cln
+ echo TERMINATE ;
+ 1>> /tmp/tmpfile_cln
+ [[ DAYA != OPSDM002 ]]
+ cat /tmp/tmpfile_cln
+ sed -e s/DAYA/OPSDM002/g
+ 1> dim_date.20080513.sql
+ typeset -l source_schema
+ typeset -l target_schema
+ cat dim_date.20080513.sql
+ sed -e s/daya/opsdm002/g
+ 1> /tmp/tmpfile_cln
+ cat dim_date.20080513.sql
+ 1> /tmp/tmpfile_cln
+ [ daya=DAYA ]
+ sed -e s/\(TEST[^"]*\)/\1_S02/g /tmp/tmpfile_cln
+ 1> dim_date.20080513.sql
+ grep VIEW dim_date.20080513.sql
CREATE VIEW OPSDM002.DIM_DATE_VIEW AS SELECT DT_SYS_ID , FULL_DT FROM OPSDM002.DIM_DATE;
CREATE VIEW OPSDM002.DIM_DATE_2 AS SELECT WEEK_BEGN_DT,QUARTER_NBR,DT_SYS_ID
+ + cut -d . -f2
+ awk /VIEW/ {print $3} dim_date.20080513.sql
VW_VAR=DIM_DATE_VIEW
DIM_DATE_2
+ echo VW_DIM_DATE_VIEW
DIM_DATE_2
VW_DIM_DATE_VIEW
DIM_DATE_2
+ sed -e s/DIM_DATE_VIEW DIM_DATE_2/VW_DIM_DATE_VIEW DIM_DATE_2/g dim_date.20080513.sql
+ 1> /tmp/tmpfile_cln
sed: Function s/DIM_DATE_VIEW cannot be parsed.
+ cat /tmp/tmpfile_cln
+ 1> dim_date.20080513.sql
+ typeset -u source_schema
+ typeset -u target_schema
+ rm /tmp/tmpfile_tab_dim_date
+ rm /tmp/tmpfile_tab_dep_dim_date
+ rm /tmp/tmpfile_cln
+ read TN
+ db2 terminate
DB20000I The TERMINATE command completed successfully.

The file comes as zero byte as the view changes are not parsed .

Thanks as always !

Looks like you have a quoting issue or something here; look into that.

Generally if you have a variable interpolation, put it in double quotes. You have judicious single quotes around parts which don't need any quoting at all, but don't quote the "hard" parts -- the variable values.

's/'"$SOME_VARIABLE"'/'"$SOME_OTHER_VARIABLE"'/g'  # actually the single quotes are completely needless
s/"$SOME_VARIABLE"/"$SOME_OTHER_VARIABLE"/g   # slightly careless, but actually equivalent
"s/$SOME_VARIABLE/$SOME_OTHER_VARIABLE/g"    # ditto

Era ,
Its stil the same . I took out the section of code with view change and hardcoded it. the sh -x is as below

sh -x view_add.shl
+ [[ -n 1 ]]
+ return
+ grep VIEW db2look_dim_provider.ddl1
CREATE VIEW OPSDM001.PROVIDER_MBR_PRI ( MBR_PRI_PROV_SYS_ID, MBR_PRI_COS_PROV_SPCL_CD,
CREATE VIEW OPSDM001.PROVIDER_REF ( REF_PROV_SYS_ID, REF_COS_PROV_SPCL_CD,
CREATE VIEW OPSDM001.PROVIDER_SRVC ( SRVC_PROV_SYS_ID, SRVC_COS_PROV_SPCL_CD,
+ + awk /VIEW/ {print $3} db2look_dim_provider.ddl1
+ cut -d . -f2
VW_VAR=PROVIDER_MBR_PRI
PROVIDER_REF
PROVIDER_SRVC
+ echo VW_PROVIDER_MBR_PRI
PROVIDER_REF
PROVIDER_SRVC
VW_PROVIDER_MBR_PRI
PROVIDER_REF
PROVIDER_SRVC
+ sed -e s/PROVIDER_MBR_PRI
PROVIDER_REF
PROVIDER_SRVC/VW_PROVIDER_MBR_PRI
PROVIDER_REF
PROVIDER_SRVC/g db2look_dim_provider.ddl1
+ 1> db2look_dim_provider.ddl2
sed: Function s/PROVIDER_MBR_PRI cannot be parsed.

Daya

Your variable contains multiple lines, is that on purpose? sed can't cope with that really.

Yes, I am trying to make a change to a ddl file which has multiple occurance of CREATE VIEW <VIEW NAME > .

How can we handle this then ?

Daya

You want it to look something like

sed 's/PROVIDER_MBR_PRI/VW_PROVIDER_MBR_PRI/
s/PROVIDER_REF/VW_PROVIDER_REF/
s/PROVIDER_SRVC/VW_PROVIDER_SRVC/' db2look_dim_provider.ddl1

Right but how do I handle this in a scenario where I don't know the number of view occurance ?

Thanks again,

Daya