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.

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.

date verification and manipulation in a shell scriptHelpful Member! 

philipose (Programmer) (OP)
12 Feb 08 15:03
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
Helpful Member!  khalidaaa (TechnicalUser)
12 Feb 08 23:07
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
philipose (Programmer) (OP)
12 Feb 08 23:19
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
mrn (MIS)
13 Feb 08 3:19
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."

mrn (MIS)
13 Feb 08 3:23
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."

philipose (Programmer) (OP)
13 Feb 08 11:23
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
philipose (Programmer) (OP)
13 Feb 08 13:00
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
philipose (Programmer) (OP)
13 Feb 08 14:56
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
khalidaaa (TechnicalUser)
14 Feb 08 1:54
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

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