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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date verification and manipulation in a shell script 1

Status
Not open for further replies.

philipose

Programmer
Dec 24, 2003
137
US
Hi gurus,
I was working on a shell script on an AIX 5.3 server. I have a parameter in the form YYYYMMDD which is in a variable within the shell script. I need to check if

i) the value in the variable YYYYMMDD is a valid date
ii) compute the date previous to it

Can anyone suggest what might be an easy way to go around it? Someone was suggesting the use of awk for ii).

thanks in advance
philipose
 
I'm sure the first thing can be done using cut -d command and then comparing it with the date command for each the year, the month and the day.

Computing the date previous to it I think can be used with the cal command (calander)

well, these are just small hints as i don't have a machine in front of me to make the code!

I will thought once i get one.

Regards,
Khalid
 
Khalid,
Thanks for your time.
The value in the variable YYYYMMDD need not be today's date. It could be any date. But I have to verify if it is a valid date. I can split it into YYYY MM DD using the cut command but that still doesnt check if the dates are correct ? ie does Feb contain the between 29 and 1 dates when leap year and 28 and 1 when not leap year, etc

Thanks
philipose
 
You can validate date using the touch command - man touch (see TIME & -t)




Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
and

Yesterday=`TZ=$TZ+24 date +%Y%m%d`
echo $Yesterday

for previous days date.

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Mike,
Thanks for your suggestions.

I think the touch -t cant validate whether the variable contains a valid date. Say the parameter I get is 20084412, we know that it is not a valid date. But touch -t just takes it as 44 months since the date.

hbuhydpf@usvh2euap0q:/home/hbuhydpf>touch -t 200844120100 autoexec.sas
hbuhydpf@usvh2euap0q:/home/hbuhydpf>ls -l autoexec.sas
-rw-r----- 1 hbuhydpf pfssas 333 Aug 12 2011 autoexec.sas

Let me try out your previous day logic
Thanks
philipose
 
Hi,
I think I got an idea to do the date check.

Say the parameter I get is in variable YYYYMMDD. I compare if this is an 8 digit number.

Then construct another variable DATECHK from YYYYMMDD in the format mmddHHMM[YYYY] and use the date command as if to set the date.
date $DATECHK 2>/tmp/somefile

Since I am not root I cant change the date anyways. If it is a valid date I will get the error message
date: 0551-003 You must be root to change the date.
If it is not a valid date I will get an error message
date: 0551-402 Invalid character in date/time specification.

Incrementing dates using perl seems to be simpler. Let me try it out
 
Hi,

The below worked for me for verifying the validity of a date. I just need to find out if a date in the format YYYYMMDD is a Sunday or not. Any thoughts


if [[ `echo $YYYYMMDD | grep "^[0-9]\{8\}$" 2>/dev/null` = "" ]]; then
echo "The parameter $YYYYMMDD should be a valid Sunday date in YYYYMMDD format"
exit 3
fi

# Split the date into Year, Month and Date
YYYY=`echo $YYYYMMDD | cut -c 1-4`
YY=`echo $YYYYMMDD | cut -c 3-4`
MM=`echo $YYYYMMDD | cut -c 5-6`
DD=`echo $YYYYMMDD | cut -c 7-8`

VALID_DATE="date: 0551-003"
date ${MM}${DD}0100${YYYY} >/dev/null 2>/tmp/$PGM_NAM
if [[ `tail -1 /tmp/$PGM_NAM | cut -c 1-14` != $VALID_DATE ]]; then
echo "The parameter $YYYYMMDD should be a valid Sunday date in YYYYMMDD format"
if [[ -e /tmp/$PGM_NAM ]]; then
rm -f /tmp/$PGM_NAM
fi
exit 4
fi
 
I'm sure that there are better way of doing it but the one i tried was this:

# thisisit = $(cal $MM $YYYY | awk '{ print $1 }' | grep $DD)

if [[ "$thisisit" != "$DD" ]]
then
echo "date is not a Sunday"
else
echo "date is a sunday"
fi

Regards,
Khalid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top