Re: Script to compare dates and notify
On Sep 11, 3:52 pm, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> littleh...@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 <filename>_%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 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 /<path_to_file>| 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
|