Hi,
Am new to shell scripting.
Please advise me as to how to write a shell script that checks a set of files under a directory, if the files are not updated/modified today then an alert mail should be sent to certain mail id.

Please help.
Am new to this technology.
Am willing to learn.

Thanks

Recommended Answers

All 11 Replies

This should do the trick:

#!/bin/bash

find /tmp/ -type f -mtime +1 -exec echo {} \; > outfile
cnt=`wc -l outfile | awk '{ print $1 }'`

if [ ${cnt} -ge "1" ] ; then
  cat outfile | /bin/mail -s "subject" user@host.com
fi

rm outfile

hi Scott,
Am getting this error for the code

Missing ]

Please help

hi Scott,
Am getting this error for the code

Missing ]

Please help

Post your code the way you wrote it to execute. Make sure there's a space between [ and ]

The script worked fine on my end.. what version of bash are you running and what distro/version?

As Aia said there may be a problem with the spacing so also post the code you used.

The script worked fine on my end.. what version of bash are you running and what distro/version?

As Aia said there may be a problem with the spacing so also post the code you used.

Hi Scott,
I use c shell.
I changed the if statement syntax, I tried everything possible but now I get this error...
if: Expression syntax

#!/bin/csh



set NOTIFY="rvytmp2"

set MAIL_FILE="/home/home17/rvytmp2/myscripts/mail_file.txt"


set dt=`date`
find . -type f -mtime +1 -exec echo {} \; > ${MAIL_FILE}

set cnt=`wc -l ${MAIL_FILE} | awk '{ print $1 }'`

echo "the count value=$cnt"

if ( ${cnt} -ge "1" )  then
Mail -s "Files not updated today ${dt}" ${NOTIFY} < ${MAIL_FILE}
fi

if (-f ${MAIL_FILE}) then
   /bin/rm -f ${MAIL_FILE}
endif

Well the script I gave you was for bash. I don't know csh scripting syntax so i'm afraid I cannot be of much help.

Thats alright Scott.

But your previous advises have been of gr8 help.

Currently, the prob is only with "if stmt" syntax error

So , please help in getting this error resolved. please give this a try.

Thanks

csh doesn't use if/fi but rather if/endif. Look at posted script and you'll see you have an if/fi.
BTW csh doesn't have the same conditional operators than bash so -ge doesn't work.

Hi Aia,
Thanks for ur advice.

I initially tried using if/endif, but it didnt work out, I got the same error.
I also used != for condition checking, but that also didnt help.

PLease help.

Thanks in advance..

Hi Aia,
Thanks for ur advice.

I initially tried using if/endif, but it didnt work out, I got the same error.
I also used != for condition checking, but that also didnt help.

PLease help.

Thanks in advance..

if ( ${cnt} -ge "1" )

That reads if cnt is greater or equal to 1 therefore != would have not worked.

if ($cnt >= 1) then
   # whatever you want to mail
endif

Thanks Aia & Scott....

my script is working now...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.