• Advertisement
Sign in to follow this  

Validating input parameters in Unix Shell Scripts

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys I have a Unix Shell script that is a wrapper to a stored procedure on an Oracle database. The script is invoked by Maestro and is passed a parameter. The parameter is a date in the format DD/MON/YYYY (uk format), and I was wondering how I should validate the format within the script. I know how to test for a single argument being passed in:
if [ $# -ne 1 ]; then

  echo 'USAGE: script_name <effective_frm_date>'
  echo         effective_frm_date format - DD/MON/YYYY'
  exit 1
fi;
But I have no idea how to test the actual format of teh parameter. Has anyone any ideas? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Look into sed and awk. Both are tools for pattern matching. It's a good idea to learn to use them anyway in case you need to write more non-trivial shell scripts in the future.

Share this post


Link to post
Share on other sites
I've discovered awk...very handy.

A quick question though...if I have the following in a shell script:


echo $DATE $LOGFILE | awk '{
if ((substr($1,3,1) != "/") || (substr($1,6,1) != "/"))
{
print "USAGE: brs1_valid_data.ksh <efftve_from_date>" > $2
print " efftve_from_date format - DD/MM/YYYY" >> $2
print " Invalid Date Format" >> $2
exit 1;
}
}'

DAY=`echo $DATE | awk '{FS="/"} {print $1}'`
MONTH=`echo $DATE | awk '{FS="/"} {print $2}'`
YEAR=`echo $DATE | awk '{FS="/"} {print $3}'`

echo $DAY $MONTH $YEAR $TODAY_DAY $TODAY_MONTH $TODAY_YEAR $LOGFILE | awk '{
if(($3 > $6) ||
(($3 == $6) && ($2 > $5)) ||
(($3 == $6) && ($2 == $5) && ($1 > $4)))
{
print "USAGE: brs1_valid_data.ksh <efftve_from_date>" > $7
print " efftve_from_date format - DD/MM/YYYY" >> $7
print " Date cannot be in future" >> $7
exit 1;
}
}'



Will the exit(s) within the awk commands exit from the script OR simply from the awk command?

EDIT: Okay have realised that I need to check the $? value after the awk commands.

[Edited by - garyfletcher on January 19, 2006 10:11:26 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement