1836221 Members
3250 Online
110096 Solutions
New Discussion

Script help

 
SOLVED
Go to solution
Anthony khan
Frequent Advisor

Script help

Hi, I need to write a script which will look for a file in dir and if the file is there and the size is not zero then it will email to the group, Can someone help how to do it... Thanks in advance
10 REPLIES 10
Steve Lewis
Honored Contributor
Solution

Re: Script help

The test you want is this:

if [ -s filename ]
then
cat filename | mail username
fi


I just tested it with an empty file, then a non-zero file. Only the non-zero file was emailed.

Hope this helps.

harry d brown jr
Honored Contributor

Re: Script help

First test this:

# cd /tmp
# touch AAA1
# touch AAA2
# echo hello>AAA2
# ls -l AAA*
-rw-rw-rw- 1 root sys 0 Mar 27 08:28 AAA1
-rw-rw-rw- 1 root sys 6 Mar 27 08:29 AAA2
# find /tmp -type f -name "AAA*" -size +0c
/tmp/AAA2
# find /tmp -type f -name "AAA*" -size +0c -exec ls -l {} \; | mail SOMEUSE@SOMEWHERE.com

THEN, create a simple script with this in it:

find /tmp -type f -name "AAA*" -size +0c -exec ls -l {} \; | mail SOMEUSE@SOMEWHERE.com


live free or die
harry
Live Free or Die
Francois Bariselle_3
Regular Advisor

Re: Script help

Hy!

Try this:
cd /net/bin > /dev/null
l -1 >! fic
foreach file ('cat fic')
if ( (! -z ${file} && (-e ${file}) ) then

...
...
...

endif
end

FRANK.
Fais la ...
Andreas Voss
Honored Contributor

Re: Script help

Hi,

you could such like this:

DIR=/directory
FILE=file

while :
do
if [ -s $DIR/$FILE ]
then
cat $DIR/$FILE | mailx user@domain
mv $DIR/$FILE $DIR/$FILE.old
fi
sleep 10
done

This will look every 10 seconds for file and if it has size > 0 it will be mailed and then renamed.

Regards
Shannon Petry
Honored Contributor

Re: Script help

Try looking at "man test"!

This gives you all of the possible switches you can give to an "if" statement.
I.E.
if [ -n FILE ] ; then
echo "the file exists and is not zero size".
elif [ -z FILE ] ; then
echo "the file exists and is zero size"
fi

Note that to properly do this, you should embed this in a check for the file first! I.E.
if [ -f FILE ] ; then
if [ -n FILE ] ; then
echo "file is there, and has size <0"
else
echo "file is there, but has 0 size"
fi
else
echo "the file is not there".
fi

Again, man pages are your friends, but it can be hard to know where to look!

Regards,
Shannon
Microsoft. When do you want a virus today?
Anthony khan
Frequent Advisor

Re: Script help

Thanks everyone for your suggestions one more thing, is there any way I can limit my search in /playpan/data, because there are some subdir in /playpen/data which i don't wanna include in search.
Ceesjan van Hattum
Esteemed Contributor

Re: Script help

If you thank all replies by words, why not assigning points ????
Anthony khan
Frequent Advisor

Re: Script help

HI Ceesjan, thanks for reminding me I am still working on script, once its done i will assign points to every one including you.

Thanks
harry d brown jr
Honored Contributor

Re: Script help



find /playpen/data -type f -name "AAA*" -size +0c -exec ls -l {} \; | grep -v -e "\/somedirA/\" -e "\/somedirB\/" -e "\/somedirC\/" | mail SOMEUSE@SOMEWHERE.com


live free or die
harry
Live Free or Die
harry d brown jr
Honored Contributor

Re: Script help

#!/usr/bin/ksh
#
DIR2SRCH="/playpen/data"
FILE2FIND="AAA*"
EXCLUDETHESEDIRS='-e \/uuuu\/ -e \/yyyy\/ -e \/zzzz\/'
#
find ${DIR2SRCH} -type f -name "${FILE2FIND}" -size +0c -exec ls -l {} \; | grep
-v ${EXCLUDETHESEDIRS} | mail SOMEUSE@SOMEWHERE.com


in a script

live free or die
harry
Live Free or Die