Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking that date has MMDDYYYY format 1

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi all,

This relates to the closed thread thread822-1501717.
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.
 
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.
 
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.
 
$? 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.
 

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top