File exists routine


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File exists routine
# 1  
Old 12-11-2008
File exists routine

Hello experts,

I need some help here..

I've written the following routine to check for existence of files. The routine does the following. It will look for a compressed ( .Z ) file and if it exists, then it will uncompress it, if it is already uncompressed, then it will just diplay a message saying "Already compressed" or else it will display the message saying "File doesn't exist". But I am sort of doing into infinite loop here.. Output below. Please can you help ? Your help is really appreciated.


uncompress () {

PARFILE=/gbwaaposoa2q4/ora01/app/oracle/orajobs/metadata
cat ${PARFILE} | while read line
do
TABNAME=`echo $line | awk '{print $1}'`
PART=`echo $line | awk '{print $2}'`
REF=`echo $line | awk '{print $3}'`

echo "Table name is ${TABNAME}"
echo "partition name is ${PART}"
echo "Business ref is ${REF}"

DMPFILE=${TABNAME}_PRT_${PART}.dmp
echo "exp dir is $EXPDIR "
cd ${EXPDIR}
if [ -f ${EXPDIR}/${DMPFILE}.Z ]; then
uncompress ${DMPFILE}.Z
elif [ -f ${EXPDIR}/${DMPFILE}]; then
echoLog "Dump file ${DMPFILE} exists on disk. Already uncompressed. "
else
echoLog "Dump file ${DMPFILE} does not exist. "
fi
done
}


My PARFILE is as below

QSFQ1> more metadata
SOACOE_LOGGER_SUMMARY 24112008 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
SOACOE_AUDIT 24112008 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
SOACOE_AUDIT 01122008 'S3DSenderESCP_v1.2.0:MsgId=1155460108'
SOACOE_AUDIT 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=163823082'
SOACOE_AUDIT 24112008 'CoPeRSender_v1.2.0:SalesEntity_ID=165751555'
SOACOE_AUDIT 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=200324'
SOACOE_AUDIT 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=608834'
SOACOE_LOGGER_SUMMARY 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=163823082'
SOACOE_LOGGER_SUMMARY 24112008 'CoPeRSender_v1.2.0:SalesEntity_ID=165751555'
SOACOE_LOGGER_SUMMARY 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=200324'
SOACOE_LOGGER_SUMMARY 24112008 'CoPeRSender_v1.2.0:Organization_CoPeR_ID=608834'



I get the following output when i run this..

./restore_data.ksh[188]: [: missing ]
08:40:04 Dump file SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp does not exist.
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
./restore_data.ksh[188]: [: missing ]
08:40:04 Dump file SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp does not exist.
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
./restore_data.ksh[188]: [: missing ]
08:40:04 Dump file SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp does not exist.
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
./restore_data.ksh[188]: [: missing ]
08:40:04 Dump file SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp does not exist.
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp





Thanks for your help in advance.

G
# 2  
Old 12-11-2008
please use code tags
Code:
uncompress () {

	PARFILE=/gbwaaposoa2q4/ora01/app/oracle/orajobs/metadata
	cd ${EXPDIR}
	while read TABNAME PART REF  dummy
	do
	  DMPFILE=${TABNAME}_PRT_${PART}.dmp
	  if [[ ! -f ${DMPFILE} && ! -f ${DMPFILE}.Z ]] ; then
	        echo "${DMPFILE} does not exist"	  
	        continue
	  fi
               [ -f ${DMPFILE} ] && echo "${DMPFILE} already exists uncompressed"	  
               [ -f ${DMPFILE}.Z ] && uncompress ${DMPFILE}.Z  && echo "uncompressed ${DMPFILE}"
	 
	done < $PARFILE
}

start with something like this
# 3  
Old 12-11-2008
Jim,

Thanks very much.

I modified the routine as below. I know not exactly to your recommendations. But why does it run into a infinite loop ? Please can you explain ?


HTML Code:
uncompress () {

PARFILE=/gbwaaposoa2q4/ora01/app/oracle/orajobs/metadata
cat ${PARFILE} | while read line
do
        TABNAME=`echo $line | awk '{print $1}'`
        PART=`echo $line | awk '{print $2}'`
        REF=`echo $line | awk '{print $3}'`

        echo "Table name is ${TABNAME}"
        echo "partition name is ${PART}"
        echo "Business ref is ${REF}"

        DMPFILE=${EXPDIR}/${TABNAME}_PRT_${PART}.dmp
        echo "exp dir is $EXPDIR "
        if [[ ! -f ${DMPFILE} && ! -f ${DMPFILE}.Z ]] ; then
                echoLog "Dump file ${DMPFILE} does not exist. "
                continue
        fi

        [ -f ${DMPFILE} ] && echoLog "${DMPFILE} already exists uncompressed"
        [ -f ${DMPFILE}.Z ] && uncompress ${DMPFILE}.Z  && echoLog "uncompressed ${DMPFILE}"

done
}
I was thinking that it should read one line at a time until the end of the file


Got this output

Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
09:44:23 /tmp/SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp already exists uncompressed
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
09:44:23 /tmp/SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp already exists uncompressed
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
09:44:23 /tmp/SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp already exists uncompressed
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
09:44:23 /tmp/SOACOE_LOGGER_SUMMARY_PRT_24112008.dmp already exists uncompressed
Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
Table name is SOACOE_LOGGER_SUMMARY
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp

Thanks in advance.
# 4  
Old 12-11-2008
Please can someone help ?
# 5  
Old 12-11-2008
I just tested and it works fine if I comment the last one.. Any idea why I get into a infinite loop ?

HTML Code:
uncompress () {

PARFILE=/gbwaaposoa2q4/ora01/app/oracle/orajobs/metadata
cat ${PARFILE} | while read line
do
        TABNAME=`echo $line | awk '{print $1}'`
        PART=`echo $line | awk '{print $2}'`
        REF=`echo $line | awk '{print $3}'`

        echo "Table name is ${TABNAME}"
        echo "partition name is ${PART}"
        echo "Business ref is ${REF}"

        DMPFILE=${TABNAME}_PRT_${PART}.dmp
        cd $EXPDIR
        echo "exp dir is $EXPDIR "
        if [[ ! -f ${DMPFILE} && ! -f ${DMPFILE}.Z ]]; then
                echoLog "Dump file ${DMPFILE} does not exist. "
                continue
        fi

        [[ -f ${DMPFILE} ]] && echoLog "${DMPFILE} already exists uncompressed"
#        [[ -f ${DMPFILE}.Z ]] && uncompress ${DMPFILE}.Z  && echoLog "uncompressed ${DMPFILE}"

done
}
Thanks in advance

G
# 6  
Old 12-11-2008
Quote:
Originally Posted by kamathg

I modified the routine as below. I know not exactly to your recommendations. But why does it run into a infinite loop ? Please can you explain ?


Code:
uncompress () {

PARFILE=/gbwaaposoa2q4/ora01/app/oracle/orajobs/metadata
cat ${PARFILE} | while read line


You don't need cat. Use redirection:

Code:
while read line
do
  : ....
done < "$PARFILE"

Quote:
Code:
do
        TABNAME=`echo $line | awk '{print $1}'`
        PART=`echo $line | awk '{print $2}'`
        REF=`echo $line | awk '{print $3}'`
        echo "Table name is ${TABNAME}"
        echo "partition name is ${PART}"
        echo "Business ref is ${REF}"


You don't need awk. Using it three times for every line of the file will be excruciatingly slow.

Code:
while read TARNAME PART REF 
do
        echo "Table name is ${TABNAME}"
        echo "partition name is ${PART}"
        echo "Business ref is ${REF}"

Quote:
Code:
        DMPFILE=${EXPDIR}/${TABNAME}_PRT_${PART}.dmp
        echo "exp dir is $EXPDIR "
        if [[ ! -f ${DMPFILE} && ! -f ${DMPFILE}.Z ]] ; then
                echoLog "Dump file ${DMPFILE} does not exist. "
                continue
        fi

        [ -f ${DMPFILE} ] && echoLog "${DMPFILE} already exists uncompressed"
        [ -f ${DMPFILE}.Z ] && uncompress ${DMPFILE}.Z  && echoLog "uncompressed ${DMPFILE}"

done
}

I was thinking that it should read one line at a time until the end of the file

Yes, it does.
Quote:

Got this output

Table name is SOACOE_AUDIT
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp
...
partition name is 24112008
Business ref is 'CoPeRSender_v1.2.0:SalesEntity_ID=160598319'
exp dir is /tmp

Please explain what is wrong with that output.
# 7  
Old 12-15-2008
Hello,

Thanks for getting back..

the error I am hitting is this..

HTML Code:
     DMPFILE=${EXPDIR}/${TABNAME}_PRT_${PART}.dmp
        echo "exp dir is $EXPDIR "
        if [[ ! -f ${DMPFILE} && ! -f ${DMPFILE}.Z ]] ; then
                echoLog "Dump file ${DMPFILE} does not exist. "
                continue
        fi

        [ -f ${DMPFILE} ] && echoLog "${DMPFILE} already exists uncompressed"
        [ -f ${DMPFILE}.Z ] && uncompress ${DMPFILE}.Z  && echoLog "uncompressed ${DMPFILE}"

With the first if command it works fine but if i uncomment the second if, where i uncompress the file, it goes into infinite loop. I've changed it to if condition but i get the same output..

Do you know that's going wrong ?

Thanks

Regards,
G
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

File exists or not

Dear All, I am facing a small issue in my code where in I am searching for a file in a directory and if found it sends me a message along with the time and filename. However if a file is not found based on the search pattern, the result which I am getting is all the files present in that... (7 Replies)
Discussion started by: grvk101
7 Replies

2. Shell Programming and Scripting

Search if file exists for a file pattern stored in array

Hi experts, I have two arrays one has the file paths to be searched in , and the other has the files to be serached.For eg searchfile.dat will have abc303 xyz123 i have to search for files that could be abc303*.dat or for that matter any extension . abc303*.dat.gz The following code... (2 Replies)
Discussion started by: 100bees
2 Replies

3. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

4. Windows & DOS: Issues & Discussions

Script that, if file exists in Samba share, moves file to Unix server

I'm looking to do pretty much what the title says. I want a script that runs, it can run on Unix or Windows, doesn't matter, and searches a Samba shares for a .txt file. If the file exists, the script will move (or possibly copy) the file from the Samba share into a directory on our Unix... (3 Replies)
Discussion started by: twcostello
3 Replies

5. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

6. Shell Programming and Scripting

Newbie.. Find if a file exists and open, if not create the desired file..

Hey all, I'm brand new to script writing, I'm wanting to make a script that will ask for a file and then retrieve that file if it exists, and if it doesn't exist, create the file with the desired name, and I'm completely stuck.. so far.. #! bin/bash echo "Enter desired file" read "$file" if ... (5 Replies)
Discussion started by: Byrang
5 Replies

7. Shell Programming and Scripting

Need help in writing a routine for sorting a CSV file

Hi, I have a CSV file in following manner: server1,env1,patch1 server1,env1,patch2 server1,env1,patch3 server1,env2,patch1 server1,env2,patch3 server2,env3,patch1 server2,env3,patch5 server2,env4,patch1 server3,env6,patch1 server3,env7,patch2 server3,env7,patch3 I want to... (6 Replies)
Discussion started by: avikaljain
6 Replies

8. Shell Programming and Scripting

Grep pattern from different file and display if it exists in the required file

Hi, I have two files say xxx.txt and yyy.txt. xxx.txt is with list of patterns within double quotes. Eg. "this is the line1" "this is the line2" The yyy.txt with lot of lines. eg: "This is a test message which contains rubbish information just to fill the page which is of no use. this is... (3 Replies)
Discussion started by: abinash
3 Replies

9. Shell Programming and Scripting

Need to write a script in UNIX to find a file if another file exists

So I have a lot of Java applications on my servers all having their own folder from the applications subdirectory. Now, I need to do the following. Search all the applications subdirectories for message.jar. If the message.jar file exists, I need to search the application directory for... (1 Reply)
Discussion started by: mmdawg
1 Replies

10. Shell Programming and Scripting

Check File Exists and compare to previous day file script

We have data files that are ftp'd every morning to a SUN server. The file names are exactly the same except for that each has the date included in its name. I have to write script to do 2 things: STEP 1) Verify that the file arrived in morning. STEP 2) Compare the file size of the current... (3 Replies)
Discussion started by: rbknisely
3 Replies
Login or Register to Ask a Question