Home » Infrastructure » Unix » exception case (unix)
exception case [message #590766] Mon, 22 July 2013 15:36 Go to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Hi ,
Following script is trying to email report after executing pl/sql procedure by passing commandline argument SALE_LOC while running the shell script.So far it is working fine.Issue is when by mistake commandline argument is not passed it should print error as "please give sale_loc as input parameter".How can i write this?
################################################################################
#####
SALE_LOC=$1
################################################################################
#####
# Variable Declarations
################################################################################
#####

. ~/env_vars
#JOB_NAME=" Report by Date"
REPORT_DIR=$INOUT
OUTPUT_FILE="report.txt"
REPORT_FILE="report.csv"

function email_report
{

ux2dos $REPORT_DIR/$OUTPUT_FILE > $REPORT_DIR/$REPORT_FILE
mailx -s "Report by Date " ${CONFIRMATION_EMAIL} <<- EOF
Please find the attached Report
~< ! uuencode $REPORT_DIR/$REPORT_FILE report.csv
EOF

}
# Execute the Oracle procedure.
#############################################################################
function execute_sql {
echo "Running SQL Script"
sqlplus -s ${ORACLE_CONNECT}<<- EOF
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
exec prddctn.Reports.report_allcntries($SALE_LOC);
EOF
return $?
}
#main
execute_sql
STATUS=$?

if [ $STATUS -eq 0 ]
then
      echo "Mailing report to user"
      email_report
fi



Thanks
Re: exception case [message #590768 is a reply to message #590766] Mon, 22 July 2013 15:42 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
magvivek@kalyani#cat somescript
((!$#)) && echo No input parameter ! && exit 1

magvivek@kalyani#./somescript someInput
magvivek@kalyani#./somescript
No input parameter !
magvivek@kalyani#
Re: exception case [message #590774 is a reply to message #590768] Mon, 22 July 2013 15:57 Go to previous messageGo to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Hi,
thanks for the quick reply.I am new to unix shell scripting.So how does the statement stated by you
((!$#)) && echo No input parameter ! && exit 1
goes in below block .Can you please help.
# Execute the Oracle procedure.
#############################################################################
function execute_sql {
echo "Running SQL Script"
sqlplus -s ${ORACLE_CONNECT}<<- EOF
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
exec prddctn.Reports.report_allcntries($SALE_LOC);
EOF
return $?
}

Thanks
Re: exception case [message #590778 is a reply to message #590774] Mon, 22 July 2013 16:11 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10707
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
Right after your variable declarations.
You are only interested in the first input ($1 which is SALE_LOC).
All you do is, if there are no input parameters, exit.
Something like this or you can get fancier by checking for bogus/invalid input or whatever.
magvivek@kalyani#cat somescript
((!$#)) && echo No input parameter ! && exit 1
function execute_sql {
echo "Running SQL Script"
sqlplus -s ${ORACLE_CONNECT}	<<- EOF
whenever sqlerror exit sql.sqlcode;
whenever oserror exit failure;
exec prddctn.Reports.report_allcntries($SALE_LOC);
EOF
return $?
}
magvivek@kalyani#./somescript thisInput
magvivek@kalyani#./somescript
No input parameter !

[Updated on: Mon, 22 July 2013 16:23]

Report message to a moderator

Re: exception case [message #590781 is a reply to message #590778] Mon, 22 July 2013 16:29 Go to previous messageGo to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Thanks a lot for reply.It is working now.
Re: exception case [message #590782 is a reply to message #590778] Mon, 22 July 2013 16:38 Go to previous messageGo to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Hi ,Thanks for the reply.I would like to know what does ((!$#)) exactly mean.I know that $? in unix means return code from last command issued.
Re: exception case [message #590783 is a reply to message #590782] Mon, 22 July 2013 16:43 Go to previous messageGo to next message
swapnabpnn
Messages: 96
Registered: December 2010
Member
Never mind,i got it.

$# means the total number of parameters passed to any shell script.
Re: exception case [message #590795 is a reply to message #590782] Tue, 23 July 2013 00:17 Go to previous message
Littlefoot
Messages: 21806
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Quote:
I would like to know what does ((!$#)) exactly mean (...) Never mind,i got it.


Oh! I thought that Mahesh was kind of angry.

  • Attachment: swear.JPG
    (Size: 10.51KB, Downloaded 1238 times)
Previous Topic: reboot showing a blank screen
Next Topic: if record count is zero in file,need to report
Goto Forum:
  


Current Time: Thu Mar 28 18:39:03 CDT 2024