Script to compare dates and notify

Script to compare dates and notify

am 11.09.2007 21:12:17 von littlehelphere

I am trying to put together a script to compare the dates of a file
and send out an email notification if the date on the file is beyond
30 calendar days. However, I am having a bit of trouble. The file
format is _%m%d%y. So for example
foobar1_090207
foorbar2_090307
foorbar3_091507
I parse the file so I am just left with the date extension from
there I want to be notified of any file over the 30 day mark.

I tried using expr and setting up a variable for the 30 days but its
not working. Any help would be appreciated. Thanks.

Re: Script to compare dates and notify

am 11.09.2007 21:37:52 von Cyrus Kriticos

littlehelphere@gmail.com wrote:
> I am trying to put together a script to compare the dates of a file
> and send out an email notification if the date on the file is beyond
> 30 calendar days. However, I am having a bit of trouble. The file
> format is _%m%d%y. So for example
> foobar1_090207
> foorbar2_090307
> foorbar3_091507
^^
future? typo?

> I parse the file so I am just left with the date extension from
> there I want to be notified of any file over the 30 day mark.
>
> I tried using expr and setting up a variable for the 30 days but its
> not working. Any help would be appreciated. Thanks.

[bash]


$ FILENAME="foobar1_090207"
$ TIME="${FILENAME#*_}"

$ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
$ NOW="$(date +'%s')"

$ let DIFF=(NOW-PAST)/60/60/24
$ echo "diff in days: $DIFF"
diff in days: 9

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

Re: Script to compare dates and notify

am 11.09.2007 21:52:21 von Cyrus Kriticos

littlehelphere@gmail.com wrote:
> I am trying to put together a script to compare the dates of a file
> and send out an email notification if the date on the file is beyond
> 30 calendar days. However, I am having a bit of trouble. The file
> format is _%m%d%y. So for example
> foobar1_090207
> foorbar2_090307
> foorbar3_091507
^^
future? typo?

> I parse the file so I am just left with the date extension from
> there I want to be notified of any file over the 30 day mark.
>
> I tried using expr and setting up a variable for the 30 days but its
> not working. Any help would be appreciated. Thanks.

[bash]


$ FILENAME="foobar1_090207"
$ TIME="${FILENAME#*_}"

$ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
$ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')

$ let DIFF=(NOW-PAST)/60/60/24
$ echo "diff in days: $DIFF"
diff in days: 9

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

Re: Script to compare dates and notify

am 11.09.2007 21:59:19 von littlehelphere

On Sep 11, 3:52 pm, Cyrus Kriticos
wrote:
> littlehelph...@gmail.com wrote:
> > I am trying to put together a script to compare the dates of a file
> > and send out an email notification if the date on the file is beyond
> > 30 calendar days. However, I am having a bit of trouble. The file
> > format is _%m%d%y. So for example
> > foobar1_090207
> > foorbar2_090307
> > foorbar3_091507
>
> ^^
> future? typo?
>
> > I parse the file so I am just left with the date extension from
> > there I want to be notified of any file over the 30 day mark.
>
> > I tried using expr and setting up a variable for the 30 days but its
> > not working. Any help would be appreciated. Thanks.
>
> [bash]
>
> $ FILENAME="foobar1_090207"
> $ TIME="${FILENAME#*_}"
>
> $ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>
> $ let DIFF=(NOW-PAST)/60/60/24
> $ echo "diff in days: $DIFF"
> diff in days: 9
>
> --
> Best regards | "The only way to really learn scripting is to write
> Cyrus | scripts." -- Advanced Bash-Scripting Guide

Quick question- are you using Solaris? I don;t have the '-d' flag for
the format.

Re: Script to compare dates and notify

am 11.09.2007 22:15:31 von William James

On Sep 11, 2:12 pm, littlehelph...@gmail.com wrote:
> I am trying to put together a script to compare the dates of a file
> and send out an email notification if the date on the file is beyond
> 30 calendar days. However, I am having a bit of trouble. The file
> format is _%m%d%y. So for example
> foobar1_090207
> foorbar2_090307
> foorbar3_091507
> I parse the file so I am just left with the date extension from
> there I want to be notified of any file over the 30 day mark.
>
> I tried using expr and setting up a variable for the 30 days but its
> not working. Any help would be appreciated. Thanks.

#!ruby
require 'date'
Dir['foobar*'].each{|f|
f =~ /(\d\d)(\d\d)(\d\d)$/
date = Date.parse( "20"+[$3,$1,$2].join("-") )
puts f if (Date.today - date) > 30
}

Re: Script to compare dates and notify

am 11.09.2007 22:32:35 von Cyrus Kriticos

littlehelphere@gmail.com wrote:
> Cyrus Kriticos wrote:
>> [bash]
>>
>> $ FILENAME="foobar1_090207"
>> $ TIME="${FILENAME#*_}"
>>
>> $ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
>> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>>
>> $ let DIFF=(NOW-PAST)/60/60/24
>> $ echo "diff in days: $DIFF"
>> diff in days: 9
>>
>> --
>> Best regards | "The only way to really learn scripting is to write
>> Cyrus | scripts." -- Advanced Bash-Scripting Guide
>
> Quick question- are you using Solaris? I don;t have the '-d' flag for
> the format.

Linux, sorry.

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

Re: Script to compare dates and notify

am 12.09.2007 01:19:13 von Janis Papanagnou

Cyrus Kriticos wrote:
> littlehelphere@gmail.com wrote:
>
>> I am trying to put together a script to compare the dates of a file
>> and send out an email notification if the date on the file is beyond
>> 30 calendar days. However, I am having a bit of trouble. The file
>> format is _%m%d%y. So for example
>> foobar1_090207
>> foorbar2_090307
>> foorbar3_091507
>
> ^^
> future? typo?
>
>> I parse the file so I am just left with the date extension from
>> there I want to be notified of any file over the 30 day mark.
>>
>> I tried using expr and setting up a variable for the 30 days but its
>> not working. Any help would be appreciated. Thanks.
>
>
> [bash]
>
>
> $ FILENAME="foobar1_090207"
> $ TIME="${FILENAME#*_}"
>
> $ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')

$ NOW=$(date -d $(date +'%Y-%m-%d') +'%s') ; echo $NOW
1189552546

$ NOW=$(date +'%s') ; echo $NOW
1189552546

You're thinking too complex, I suppose. :-)
(Or any trick I am missing?)

Janis

>
> $ let DIFF=(NOW-PAST)/60/60/24
> $ echo "diff in days: $DIFF"
> diff in days: 9
>

Re: Script to compare dates and notify

am 12.09.2007 06:08:26 von Cyrus Kriticos

Janis Papanagnou wrote:
> Cyrus Kriticos wrote:
>>
>> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>
> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s') ; echo $NOW
> 1189552546
>
> $ NOW=$(date +'%s') ; echo $NOW
> 1189552546
>
> You're thinking too complex, I suppose. :-)
> (Or any trick I am missing?)

# seconds since 1970-01-01 to midnight
$ NOW=$(date -d $(date +'%Y-%m-%d') +'%s') ; echo $NOW
1189548000

# seconds since 1970-01-01 to now
$ NOW=$(date +'%s') ; echo $NOW
1189569887

$ date --version | head -n 1
date (GNU coreutils) 5.97

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

Re: Script to compare dates and notify

am 12.09.2007 15:47:18 von Janis Papanagnou

Cyrus Kriticos wrote:
>
> # seconds since 1970-01-01 to midnight
> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s') ; echo $NOW
> 1189548000
>
> # seconds since 1970-01-01 to now
> $ NOW=$(date +'%s') ; echo $NOW
> 1189569887
>
> $ date --version | head -n 1
> date (GNU coreutils) 5.97
>

So one has to take care about portability to avoid subtle errors
when using such date contructs.

$ date +'%s'
1189593776
$ date -d $(date +'%Y-%m-%d') +'%s'
1189593776
$ date -d $(date +'%Y-%m-%d+%H:%M:%S') +'%s'
1189593776
$ date -d $(date +'%Y-%m-%d+00:00:00') +'%s'
1189548000
$ date --version
version date (AT&T Research) 2005-03-07

AT&T date assumes current date/time if no date/time is specified.

# GNU
$ gdate -d $(gdate +'%Y-%m-%d') +'%s'
1189548000


$ gdate -d $( gdate +'%H:%M:%S' )
Mit Sep 12 12:42:56 CEST 2007
$ gdate -d $( gdate +'%Y-%m-%d' )
Mit Sep 12 00:00:00 CEST 2007

GNU date assumes time is 00:00:00 if none specified, but assumes
current date if no date is specified.


More on GNU date (locales)...

$ gdate -d "$( gdate )"
gdate: invalid date `Mit Sep 12 12:42:56 CEST 2007'
$ gdate -d "$( LANG=C gdate )"
Mit Sep 12 12:42:56 CEST 2007
$ gdate --version | head -1
date (coreutils) 4.5.8

Has this been fixed in current versions of GNU date?


Janis

Re: Script to compare dates and notify

am 12.09.2007 16:17:44 von littlehelphere

On Sep 11, 3:52 pm, Cyrus Kriticos
wrote:
> littlehelph...@gmail.com wrote:
> > I am trying to put together a script to compare the dates of a file
> > and send out an email notification if the date on the file is beyond
> > 30 calendar days. However, I am having a bit of trouble. The file
> > format is _%m%d%y. So for example
> > foobar1_090207
> > foorbar2_090307
> > foorbar3_091507
>
> ^^
> future? typo?
>
> > I parse the file so I am just left with the date extension from
> > there I want to be notified of any file over the 30 day mark.
>
> > I tried using expr and setting up a variable for the 30 days but its
> > not working. Any help would be appreciated. Thanks.
>
> [bash]
>
> $ FILENAME="foobar1_090207"
> $ TIME="${FILENAME#*_}"
>
> $ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>
> $ let DIFF=(NOW-PAST)/60/60/24
> $ echo "diff in days: $DIFF"
> diff in days: 9
>
> --
> Best regards | "The only way to really learn scripting is to write
> Cyrus | scripts." -- Advanced Bash-Scripting Guide

I changed the script around a bit to search for all the files and go
through them. Problem is is only reads and works on the one file so
the dates are returning as the same. Any ideas?

#!/bin/bash
FILENAME=`ls /| grep _[0-9]` (this produces foo_090407
foo_090507 foo_090607)
for i in $FILENAME
do
TIME="${FILENAME#*_}"
PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
let DIFF=(NOW-PAST)/60/60/24
echo "$i diff in days: $DIFF"
done

The above produces
foo_090407 diff in days: 8
foo_090507 diff in days: 8
foo_090607 diff in days: 8

Re: Script to compare dates and notify

am 12.09.2007 17:05:00 von Bill Marcum

>
> Cyrus Kriticos wrote:
>>
>> # seconds since 1970-01-01 to midnight
>> $ NOW=$(date -d $(date +'%Y-%m-%d') +'%s') ; echo $NOW
>> 1189548000
>>
>> # seconds since 1970-01-01 to now
>> $ NOW=$(date +'%s') ; echo $NOW
>> 1189569887
>>
The original poster in this thread said she does not have 'date -d'.
She might use the links found here (thanks to Chris Johnson, Stephane
Chazelas et al):
http://cfaj.freeshell.org/shell/cus-faq.html#6


--
Kent's Heuristic:
Look for it first where you'd most like to find it.

Re: Script to compare dates and notify

am 12.09.2007 18:07:33 von Janis Papanagnou

littlehelphere@gmail.com wrote:
> On Sep 11, 3:52 pm, Cyrus Kriticos
> wrote:
>
>>littlehelph...@gmail.com wrote:
>>
>>> I am trying to put together a script to compare the dates of a file
>>>and send out an email notification if the date on the file is beyond
>>>30 calendar days. However, I am having a bit of trouble. The file
>>>format is _%m%d%y. So for example
>>>foobar1_090207
>>>foorbar2_090307
>>>foorbar3_091507
>>
>> ^^
>> future? typo?
>>
>>
>>> I parse the file so I am just left with the date extension from
>>>there I want to be notified of any file over the 30 day mark.
>>
>>>I tried using expr and setting up a variable for the 30 days but its
>>>not working. Any help would be appreciated. Thanks.
>>
>>[bash]
>>
>>$ FILENAME="foobar1_090207"
>>$ TIME="${FILENAME#*_}"
>>
>>$ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
>>$ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>>
>>$ let DIFF=(NOW-PAST)/60/60/24
>>$ echo "diff in days: $DIFF"
>>diff in days: 9
>>
>>--
>> Best regards | "The only way to really learn scripting is to write
>> Cyrus | scripts." -- Advanced Bash-Scripting Guide
>
>
> I changed the script around a bit to search for all the files and go
> through them. Problem is is only reads and works on the one file so
> the dates are returning as the same. Any ideas?
>
> #!/bin/bash
> FILENAME=`ls /| grep _[0-9]` (this produces foo_090407
> foo_090507 foo_090607)
> for i in $FILENAME
> do
> TIME="${FILENAME#*_}"

TIME="${i#*_}"

Janis

> PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
> let DIFF=(NOW-PAST)/60/60/24
> echo "$i diff in days: $DIFF"
> done
>
> The above produces
> foo_090407 diff in days: 8
> foo_090507 diff in days: 8
> foo_090607 diff in days: 8
>

Re: Script to compare dates and notify

am 12.09.2007 19:02:09 von littlehelphere

On Sep 12, 12:07 pm, Janis Papanagnou
wrote:
> littlehelph...@gmail.com wrote:
> > On Sep 11, 3:52 pm, Cyrus Kriticos
> > wrote:
>
> >>littlehelph...@gmail.com wrote:
>
> >>> I am trying to put together a script to compare the dates of a file
> >>>and send out an email notification if the date on the file is beyond
> >>>30 calendar days. However, I am having a bit of trouble. The file
> >>>format is _%m%d%y. So for example
> >>>foobar1_090207
> >>>foorbar2_090307
> >>>foorbar3_091507
>
> >> ^^
> >> future? typo?
>
> >>> I parse the file so I am just left with the date extension from
> >>>there I want to be notified of any file over the 30 day mark.
>
> >>>I tried using expr and setting up a variable for the 30 days but its
> >>>not working. Any help would be appreciated. Thanks.
>
> >>[bash]
>
> >>$ FILENAME="foobar1_090207"
> >>$ TIME="${FILENAME#*_}"
>
> >>$ PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> >>$ NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
>
> >>$ let DIFF=(NOW-PAST)/60/60/24
> >>$ echo "diff in days: $DIFF"
> >>diff in days: 9
>
> >>--
> >> Best regards | "The only way to really learn scripting is to write
> >> Cyrus | scripts." -- Advanced Bash-Scripting Guide
>
> > I changed the script around a bit to search for all the files and go
> > through them. Problem is is only reads and works on the one file so
> > the dates are returning as the same. Any ideas?
>
> > #!/bin/bash
> > FILENAME=`ls /| grep _[0-9]` (this produces foo_090407
> > foo_090507 foo_090607)
> > for i in $FILENAME
> > do
> > TIME="${FILENAME#*_}"
>
> TIME="${i#*_}"
>
> Janis
>
> > PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> > NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
> > let DIFF=(NOW-PAST)/60/60/24
> > echo "$i diff in days: $DIFF"
> > done
>
> > The above produces
> > foo_090407 diff in days: 8
> > foo_090507 diff in days: 8
> > foo_090607 diff in days: 8

That looks to be the solution. Thanks. Also, for my own reference
what does ${i#*_} expand to? Obviously $i. But what about the bit
after? Thanks.

Re: Script to compare dates and notify

am 12.09.2007 19:05:00 von Bill Marcum

On Wed, 12 Sep 2007 14:17:44 -0000, littlehelphere@gmail.com
wrote:
>
>
> I changed the script around a bit to search for all the files and go
> through them. Problem is is only reads and works on the one file so
> the dates are returning as the same. Any ideas?
>
> #!/bin/bash
> FILENAME=`ls /| grep _[0-9]` (this produces foo_090407
> foo_090507 foo_090607)
> for i in $FILENAME
> do
> TIME="${FILENAME#*_}"
TIME="${i#*_}"

> PAST="$(date -d "20${TIME:4:2}-${TIME:0:2}-${TIME:2:2}" +'%s')"
> NOW=$(date -d $(date +'%Y-%m-%d') +'%s')
> let DIFF=(NOW-PAST)/60/60/24
> echo "$i diff in days: $DIFF"
> done
>
> The above produces
> foo_090407 diff in days: 8
> foo_090507 diff in days: 8
> foo_090607 diff in days: 8
>


--
Include me out.

Re: Script to compare dates and notify

am 12.09.2007 21:05:01 von Bill Marcum

On Wed, 12 Sep 2007 17:02:09 -0000, littlehelphere@gmail.com
wrote:
>
>
> That looks to be the solution. Thanks. Also, for my own reference
> what does ${i#*_} expand to? Obviously $i. But what about the bit
> after? Thanks.
>
(from the zsh man page, but this applies to all POSIX shells)
${name#pattern}
${name##pattern}
If the pattern matches the beginning of the value of name, then
substitute the value of name with the matched portion deleted;
otherwise, just substitute the value of name. In the first
form, the smallest matching pattern is preferred; in the second
form, the largest matching pattern is preferred.
${name%pattern}
${name%%pattern}
[replace "beginning" with "end" in the above paragraph.]


--
BOFH excuse #376:
Budget cuts forced us to sell all the power cords for the servers.