Operating System - HP-UX
1752780 Members
6227 Online
108789 Solutions
New Discussion юеВ

Re: script to check file existence and notify

 
Ahmad Munawwar
Frequent Advisor

script to check file existence and notify

Hello,

Need help on my script. I wanted to have a script that will check the file in the directory, if not match then send e-mail to user. I have configured the mail the only thing is to make my script work.

#!/bin/sh

date=`date '+20%y-%d-%m'`

#Check if file is match in the directory

ls -ltr xyz.$date:*

#if not the send to mail

mailx -s "file not match" user@abc.foo.com << EOF
Please check your file


br,
Munawawr(script beginner)
7 REPLIES 7
Jordan Bean
Honored Contributor

Re: script to check file existence and notify

One way to do it:

#!/bin/sh
today=$(date +%Y-%m-%d)
for file in xyz.${today}:* ; do
if [ ! -f $file ] ; then
mailx -s "${file}: file not match" user@foo.com << EOF
Please check for file $file.
EOF
fi
done
Michael_356
Frequent Advisor

Re: script to check file existence and notify

Hi there,

read the manpage for 'test'.
There you can find all possibilities to check a file:
-r file True if file exists and is readable.

-w file True if file exists and is writable.

-x file True if file exists and is executable.

-f file True if file exists and is a regular file.
.
.
.
and so forth

Michael
Muthukumar_5
Honored Contributor

Re: script to check file existence and notify

We can check file exisence with [[ -f there.

Another way as, To do your work then,

#!/bin/sh

date=`date '+20%y-%d-%m'`

#Check if file is match in the directory


ls -ltr "xyz.${date}:*" 1>/dev/null 2>&1

#if not the send to mail

if [[ $? -ne 0 ]]
then
mailx -s "file not match" user@abc.foo.com << EOF
Please check your file
EOF
fi

HTH
Easy to suggest when don't know about the problem!
Ralph Grothe
Honored Contributor

Re: script to check file existence and notify

An asside to Muthukumur's reply.
Be careful with the use of the "new" test "operator" in guise of double square brackets [[ -f ... ]], especially in your intended context.
To my knowledge the "new" test construct does prevent word splitting and filename expansion.
So if you're inclined to use a shell globbing expression in a file test condition that would not work as expected
e.g.

if [[ -d /app/oracle/product/[89].[12].[07] ]]; then
# do soemthing
fi

Madness, thy name is system administration
Ahmad Munawwar
Frequent Advisor

Re: script to check file existence and notify

Hi,

I use this way and it work I got the mail:-

#!/bin/sh

#today=$(date+20%y-%d-%m)
date=`date '+20%y-%d-%m'`
for file in xyz.$date:*;do
if [ ! -f $file ];then
mailx -s "$(file): file not match" user@foo.com << EOF
Please check for file $file
EOF
fi
done

However when it found that the file not match then I got this additional message after run the script:-
usage: file [-c] [-f ffile] [-m mfile] [-h] file...

where I should touch up this?

br,
Munawawr

Marcel Boogert_1
Trusted Contributor

Re: script to check file existence and notify

Hi there,

You should use the mailx command like this:

mailx -s "$file: file not match" user@foo.com << EOF
Please check for file $file
EOF

Currently you're using command substitution. So if you type file from your prompt you get the same message.

Regards, MB.
Muthukumar_5
Honored Contributor

Re: script to check file existence and notify

You are trying to execute file command as $(file) instead of using as ${file} or $file there.

#!/bin/sh

#today=$(date+20%y-%d-%m)
date=`date '+20%y-%d-%m'`
for file in xyz.${date}:*;do
if [ ! -f $file ];then
mailx -s "${file}: file not match" user@foo.com << EOF
Please check for file $file
EOF
fi
done

It will work there.

HTH.
Easy to suggest when don't know about the problem!