Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

checking that date has MMDDYYYY formatHelpful Member! 

starlite79 (TechnicalUser)
15 May 09 17:33
Hi all,

This relates to the closed thread thread822-1501717: defining part of a string (YYMMDD date format) with shell script.
I've modified the script to get files in a range of two dates that are supplied on the command line while running the script.  I want to test that both $1 and $2 are in the format MM/DD/YYYY.  This is what I have so far:

CODE

# Define a variable for the analysis files.  Earliest analysis file for 2008
# is from January 1, 2008.  Get two date string arguments from the command
# line (i.e. $1 $2).  Dates must be in MM/DD/YYYY format.

date1="${1}"
date2="${2}"

# Get dates from standard input
if [ -n $(date +%m/%d/%Y $date1) ] && [ -n $(date +%m/%d/%Y $date2) ];  then
        echo "dates are " $date1 $date2
else
        echo "need to add two dates to input line "
        echo "  such as 08/08/2008 08/09/2008 "
        echo "  in MM/DD/YYYY format. "
        exit
fi

However, this syntax does not work.  date gives an error that there is an extra operand.  The program does work if both dates are in the correct format, but I want the else statement to execute if either or both the arguments are not given as MM/DD/YYYY as needed by the rest of the script.
Helpful Member!  Annihilannic (MIS)
17 May 09 20:47
You already have the dates from the two input parameters, so you don't need to use the date command to obtain them again.

Try instead echoing the supplied dates to grep with a suitable regular expression to match a valid date format.

Annihilannic.

starlite79 (TechnicalUser)
18 May 09 15:58
Thanks Annihilannic.

I tried something with echo and grep as you suggested, but it is not working.  Could you look over my code snippet and let me know if anything catches your attention?

CODE

# Define a variable for the analysis files.  Earliest analysis file for 2008
# is from January 1, 2008.  Get two date string arguments from the command
# line (i.e. $1 $2).  Dates must be in MM/DD/YYYY format.

date1="${1}"
date2="${2}"

# Get dates from standard input
echo "$date1 $date2" | grep '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' > /dev/null 2>&1
echo "$?"
if [ "$?" -eq "0" ]; then
        echo "dates are " $date1 $date2
else
        echo "need to add two dates to input line "
        echo "  such as 08/08/2008 08/09/2008 "
        echo "  in MM/DD/YYYY format. "
        exit
fi

If the dates are fine, the program happily goes along.  But, if the format is wrong for one or both dates, the output is still dates are ... and the program hangs.   
Annihilannic (MIS)
18 May 09 21:06
$? is a very short-lived value; every command you execute changes it, so by doing an echo $? you display what the result of the previous command was, but you also reset it to the result of the current command (the echo), so it will always be 0 since echo nearly always succeeds.

Also you'll need to check each date individually since your regexp is designed to validate a single date.

Something like this perhaps:

CODE

if echo "$date1" | grep -q '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$' &&
   echo "$date2" | grep -q '^[0-1][0-9]/[0-3][0-9]/[0-9]\{4\}$'
then
        echo "dates are " $date1 $date2
        ...

grep -q saves you having to use the ugly redirection stuff (if it is supported by your version of grep that is!), and using the commands in-line in the if statement means that their exit codes are used immediately rather than accessing them through a subsequent $?.

Annihilannic.

starlite79 (TechnicalUser)
19 May 09 11:36
This is very much appreciated!
LKBrwnDBA (MIS)
20 May 09 11:36

Nice try Annihilannic, but you let month 13-19 pass.
Better try it with "touch":

CODE

if echo $(touch -c -d "$date1" kk 2>&1)|grep ' invalid ' ||
   echo $(touch -c -d "$date2" kk 2>&1)|grep ' invalid '
then
   echo "!Error, bad date(s), use MM/DD/YYYY format."
   exit 3
else
   echo "dates are $date1 $date2"
fi
3eyes

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close