Home » Infrastructure » Unix » need to exit if report file is not found (unix)
need to exit if report file is not found [message #591106] Wed, 24 July 2013 15:54 Go to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Hi,
I have huge unix block code.In which i am taking out part of it.In this part it sends report file after checking line count in report file.So far it is good.But I also need to add condition where if the datafile is not found then it need to exit out or else continue to process and send the email.It is to find whether datafile is existing or not.How can I write this.Can anyone please help.
function email_report {
ux2dos $REPORT_DIR/$OUTPUT_FILE > $REPORT_DIR/$REPORT_FILE
DATAFILE=`ls -t1 $REPORT_DIR/$REPORT_FILE`
rows=`wc -l ${DATAFILE} | awk '{print $1}'` # line count from the data file
rows=$((rows-1));         # remove the header row from the count
if [ ${rows} -eq 0 ]
then
result="No items were there."
else
result="Please find the attached Report"
fi
BODY=`cat<<EOF
Job Name: ${JOB_NAME}
Script: $BIN/$SCRIPT_NAME
${result}
EOF`

print "emailing reports..."
mailx -s "${JOB_NAME}" ${REPORT_EMAIL} <<- EOF
$BODY
~< ! uuencode $REPORT_DIR/$REPORT_FILE report.csv
EOF
return $?
}


Thanks
Re: need to exit if report file is not found [message #591151 is a reply to message #591106] Thu, 25 July 2013 03:57 Go to previous messageGo to next message
ThomasG
Messages: 3099
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
The "check if file exists" file test operator is -e.

You might want to use "-f" though, so that a directory or device of the same name does not break stuff.

if [ -f /etc/hosts  ]
then
  echo "File is a regular file"
else 
  echo "File is not regular file or does not exist"
fi


You could also use "-s" to check if the file has non-zero size, it might be faster than the wc.

[Updated on: Thu, 25 July 2013 03:59]

Report message to a moderator

Re: need to exit if report file is not found [message #591152 is a reply to message #591106] Thu, 25 July 2013 04:16 Go to previous message
Michel Cadot
Messages: 59147
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
This question bears no real connection with Oracle database.
Please find a more appropriate forum.
The topic is locked. ./fa/448/0/
If I'm wrong, please, PM me (or report this message to a moderator, explain why you think it should be unlocked and it might be done).

Regards
Michel
Previous Topic: if record count is zero in file,need to report
Next Topic: spool error
Goto Forum:
  


Current Time: Sun Sep 21 01:53:31 CDT 2014

Total time taken to generate the page: 0.07651 seconds