Help Needed in Date Condition !!

I am writing an shell script , which reads a file with contents

Example :: (( This Format is yyyy-MM-DD))

2008-05-16
2008-05-17
2008-05-18
2008-05-19
2008-05-20
2008-05-21

when i run the shell script it has to check the current date and compare it with the todays date and give me the value which is one day less than that of todays date ..

when i execute DATE command it gives me this

Fri Mar 21 02:04:21 GMT 2008

so output should be 2008-05-20 , is there are no values then echo " no values present "

This is just to find what are all old in my unix box .. This is used in my routine work

Thanks

What do you think is the difference between Current date & Today's date?
And what are you trying to do with your input file?

Can you pls explain your problem clearly? Try and paste a bit code you've tried to develop so far..it will fetch you better & quicker replies.
Good luck!

Sorry .. i was not clear , these values are present in a file .. and i have to compare it with current date in unix box and give a result which is one day
more , and if there are no entries in the input file then result " NO values are present"

2008-05-16
2008-05-17
2008-05-18
2008-05-19
2008-05-20
2008-05-21
2008-05-22

Hi .. i am not sure of the code .. Please help me on this as i am new to this shell scripting

Thanks

You can use the following date format & use this value in any LOOP or Check condition

echo `date '+DATE:%d/%m/%Y%nTIME:%H:%M:%S'`

Pass the above date in a variable and use it..

Thanks for your replies .. but it didnot work !!

This is my input

I have a file a.txt which has the values which is YYYY-MM-DD format

[expires: 2008-03-29]
[expires: 2008-04-26]
[expires: 2008-05-26]
[expires: 2008-06-26]
[expires: 2008-07-26]

i need to chk the current date (systemdate) and compare it with a.txt if the expiring dates are with the range of 30 days it should print the line , else ignore the line and echo " no Keys are in expiring state"

I know that we can proceed on if conditons , but not sure of the date conversion .. Thanks for all your help !!

see this forum entry:

http://www.unix.com/tips-tutorials/31944-simple-date-time-calulation-bash.html

for information and some excellent routines for manipulating dates and times.

Also a read through the man page for date will help you.

a possible solution off the top of my head without testing is:

while read line; do 
$expires=`echo $line | sed 's/^.*: \(.*\)$/\1'`
$timestamp=`date --utc --date "$expires" +%s`
$nowplus30days=`date --date "now +30 days"`
[ $nowplus30days -lt $timestamp ] && echo $line || echo "Key not in expiring state"
done < a.txt

Thanks for youe help!!

but it giving sed command grabled .. can you please take a look of the below error !! Thanks again

date: illegal option -- -
date: illegal option -- t
date: illegal option -- c
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[3]: =: not found
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[4]: =: not found
[expires: 2008-03-29]
sed: command garbled: s/^.: \(.\)$/\1
cc.sh[2]: =: not found
date: illegal option -- -
date: illegal option -- t
date: illegal option -- c
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[3]: =: not found
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[4]: =: not found
[expires: 2008-04-26]
sed: command garbled: s/^.: \(.\)$/\1
cc.sh[2]: =: not found
date: illegal option -- -
date: illegal option -- t
date: illegal option -- c
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[3]: =: not found
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[4]: =: not found
[expires: 2008-05-26]
sed: command garbled: s/^.: \(.\)$/\1
cc.sh[2]: =: not found
date: illegal option -- -
date: illegal option -- t
date: illegal option -- c
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[3]: =: not found
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[4]: =: not found
[expires: 2008-06-26]
sed: command garbled: s/^.: \(.\)$/\1
cc.sh[2]: =: not found
date: illegal option -- -
date: illegal option -- t
date: illegal option -- c
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[3]: =: not found
date: illegal option -- -
date: illegal option -- d
date: invalid argument -- te
usage: date [-u] mmddHHMM[[cc]yy][.SS]
date [-u] [+format]
date -a [-]sss[.fff]
cc.sh[4]: =: not found
[expires: 2008-07-26]

Hmm, which unix are you using. I came at this from a Linux perspective with GNU Date. If you don't have GNU date then some of those commands will def. fail. Also, as I said, it was off the top of my head, so the sed command regex will probably need some work.

Have to go eat with the inlaws now, so will look at this again tonight if you haven't solved it by then.

Things that might be pertinent:

what unix? (hpux, solaris, tru64 etc)
what version of unix?
do you have access to gnu date?
which shell? (bash, korn etc)

unix version :: SunOS 5.8

Shell type :: Korn

i am not sure of the GNU version of dates here .. i am a new to this platform

i am using sun solaris and Using ksh (Korn shell)

I'm quite new to perl but you can give this a try :slight_smile:

perl -ane'BEGIN {
  @T = localtime(time+86400*30);
  $t = sprintf "%d-%02d-%02d", ($T[5]+1900),$T[4]+1,$T[3];
}
  chop $F[1]; $t lt $F[1] || print
' file

Of course, fixes and suggestions are welcome!

Thanks for the reply .. Can you also tell me , when there are no days in the range of 30 days .. it should print " no values are present

You can use something like this:

perl -ane'BEGIN {
  @T = localtime(time+86400*30);
  $t = sprintf "%d-%02d-%02d", ($T[5]+1900),$T[4]+1,$T[3];
}
chop $F[1]; 
if ($t gt $F[1]) { 
  $c++;
  print; 
}
print "No values present!\n" if (eof and !$c)
' file

Using ksh93

#!/usr/bin/ksh93

TMP=file.$$
DATESTR=$(printf "%(%Y-%m-%d)T\n" "now - 1 day")
FOUND=0

cat <<EOT >$TMP
2008-05-17
2008-05-18
2008-05-19
2008-03-22
2008-05-21
EOT

while read LINE
do
   [[ ${DATESTR} == "$LINE" ]] && FOUND=1
done < $TMP

[[ $FOUND == 0 ]] && print "no values present"

rm $TMP
exit 0

Hi .. i tried using this .. but couldnot analyze th code .. it gives an error ..

syntax error at -e line 9, near "print
"
Semicolon seems to be missing at -e line 10.
syntax error at -e line 11, near ")
a"
Execution of -e aborted due to compilation errors.

Works for me,
try to write it by hand,
instead of copy/pasting.

Thanks .. I have few more doubts in this shell script ...

1) If i have a line say for example :: I have stored the output in a file

kkkkbbbb1476ysy <jyhshsh@ffassd.com> [expires: 2008-03-28]

when i give the above shell script , can i get the entire line .. if the condition satisfies..

This would be my last question .. i should be all set after this

Thanks a lot for your help!!!!

The above problem has been solved .. once the Program finds that there is no values found .. it has to exit out from the shell and send an email alert As No key values found .. else it proceeds with the rest of the steps !!!

Thanks .. Kindly help me on this !!

Is that a question?

Your punctuation keys seem to print double every time.