I want to find a file for every 60sec. If the file exists between the time range then an email should trigger. i tried below but not working. Apprecite your help ! THanks Advance

cd /my/path
while true 
    set Hrs = `date "+H"`
    if [ $Hrs >= 17 ] && [$Hrs <=23 ]; then
        $filefound = `ls tt.txt`
        if  [ $filefound = "tt.txt" ]; then
             mailx send email......
sleep 5


Try the below code.

cd /my/path
while true; do
    Hrs=`date "+%H"`
    if [ "$Hrs" -ge 17 ] && [ "$Hrs" -le 23 ]; then
        if  [ -f tt.txt ]; then
          echo "File Found" | mail -s "This is the subject" <email address>
    sleep 60

I have tested entire script except for the mail part. Everything else works i hope mail does too. Cheers.

| improve this answer | |

There is an exact test (in all shells) to see if a file exists, is a real file, is readable, etc. Using ls is to be avoided.

.. It will send an error message every time the file isn't there.

.. It runs an external process every time.

.. This will also detect a directory, a named pipe, a device etc.

If you are going to send an email, you might as well include the file name you found.

It would be better to declare the filename once as a shell variable. It would be even better to make it an argument to the script, so it could be used for different files without change.

sleep 5 is different to sleep 60.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.