CLI script for emailing alert if files missing in dir

thread removed

Is this a homework assignment? If so, please refile in the Homework & Coursework Questions forum following the directions specified here.

If not, what have you tried?

What operating system and shell are you using?

removed

Here is example to use ksh93 shell, which include builtin date calculation properties.

In other shell you can use ex. GNU date like:

from="20140226"
epoc=$(date --date="$from" +"%s")
#!/bin/ksh93

smail()
{

/usr/lib/sendmail -t -i <<EOF
From: myemail@some.com
To: alert@some.com
Subject: Missing

$*

Thats all.

-checker-

EOF
}

####MAIN###################
epocnow=$(printf "%(%#)T" now)
nowyyyymmddhh=$(printf "%(%Y%m%d%H)T" now)

datestr=$(printf "%(%Y-%m-%d %H:00)T" now)
epocnow2=$(printf "%(%#)T" "$datestr")
# length of day (seconds) = 86400
((hour=60*60))
((day=24*hour))

startstr="2015-02-23 00:00"
epoc=$(printf "%(%#)T" "$startstr")

print "$epocnow - $nowyyyymmddhh - $epocnow2 - $datestr :: $epoc"

mailstr=""
prev=""
lf=$'\x0a'
while (( epoc < epocnow ))
do
        file=$(printf "%(%Y%m%d/%H)T" "#$epoc")
        path=$(printf "%(%Y%m%d)T" "#$epoc")
        print "$path $file"
        [ ! -d "$path" -a "$path" != "$prev" ] && prev=$path && mailstr="$mailstr${lf}path missing $path"
        [ ! -f "$file" ] && mailstr="$mailstr${lf}-file missing $file"
        (( epoc+=hour ))
done

smail "$mailstr"


1 Like

removed