1751690 Members
3132 Online
108781 Solutions
New Discussion юеВ

Re: script wanted

 
SOLVED
Go to solution
ust3
Regular Advisor

script wanted

Hi ,
I am not familiar with writing script , if I want to check a directory , to find out all the file that the files are
1. contains a word "shutdown" ( any Block or small letter ) ,
2. not elder than 3 days ,

then send mail with the file name and the whole phase of the shutdown statement .

can advise how to write the script .
thx in advance.
7 REPLIES 7
Bill Hassell
Honored Contributor

Re: script wanted

#!/usr/bin/sh

set -u
export PATH=/usr/bin
MYDIR=/changeMEtoYOURdir
cd $MYDIR
RTN=$?
[ $RTN -ne 0 ] && echo "no permission or no such directory: $MYDIR" && exit 1
FILELIST=$(find * -type f -prune -mtime -3)
[ -z "$FILELIST" ] && echo "no files less than 4 days old" && exit 2
SDLIST=$(grep -i shutdown $FILELIST)
COUNT=$(echo "$SDLIST" | wc -l)
[ $COUNT -eq 0 ] && SDLIST="none found"
[ $COUNT -eq 1 ] && SDLIST=$FILELIST:$SDLIST

echo "$SDLIST" | mailx -s "$COUNT file(s) found with shutdown" your_name@cpu.com


Bill Hassell, sysadmin
Erol KAHRAMAN
Advisor
Solution

Re: script wanted

hi
i think its not very comlicated. I wrote a simple shell script that you can try.

#!/bin/sh
PATH=searching_path
N=number_of_days
TEXT=word_you_are_searching
FILE=file_where_you_writing results
EMAIL=email_address_where_you_send_results

find $PATH -type f -ctime -$N | xargs grep -i $TEXT > $FILE

mail -s "SUBJECT" $EMAIL < $FILE
Shannon Petry
Honored Contributor

Re: script wanted

I believe by "phase" you mean the contents of the file with the word shutdown in it. Since you are new to scripting, I'll be very explicit in wording and commands so you can replicate and learn.

#!/bin/sh
PATH="/bin:/usr/bin"
MYDIR="/directory/you/want/to/scan"
# make sure we have the directory
if [ -d $MYDIR ] ; then
cd $MYDIR
if [ $? -ne 0 ] ; then
echo "could not cd to $MYDIR" ; exit 11
fi
else
echo "no such directory $MYDIR" ; exit 2
fi
# make a list of files
FILES=`find . -mtime -3 -type f -print 2>>/dev/null |grep -i shutdown`
# Do nothing if we have nothing
if [ "${FILES}x" = "x" ] ; then
echo "No files to process"
else
TMP="/tmp/my.mailfile.txt"
for FILE in $FILES ; do
echo "$FILE" >>$TMP
cat $FILE >>$TMP
echo "" >>$TMP ; echo "" >>$TMP
done
cat $TMP |mailx -s "shutdown requests" youraddr@somewhere.net
rm $TMP
fi
Microsoft. When do you want a virus today?
ust3
Regular Advisor

Re: script wanted

thx all

If I want the mail is only send when the word "error" is exist , that mean if no error then don't send the mail , can advise what can i do ? thx
Peter Nikitka
Honored Contributor

Re: script wanted

Hi,

change the 'else' part of Shannon├Г s solution to:
...
else
TMP="/tmp/my.mailfile.txt"
SEARCH4=error
for FILE in $FILES ; do
if fgrep $SEARCH4 $FILE >/dev/null
then
print "\n$FILE"
print "================="
cat $FILE
fi
done >$TMP
if [ -s $TMP ]
then
mailx -s "shutdown requests" youraddr@somewhere.net <$TMP
fi
rm $TMP
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
ust3
Regular Advisor

Re: script wanted

Thx replies .

My script is simple as below , it works find now ..

find /ora/output/ -type f -mtime -2 | xargs grep -i "error" > $FILE
cat $FILE >> $mailfile
echo "System checking" >> $mailfile
mail -f "admin@my.com" -s "System Checking" user@my.com < $mailfile


Now , I would like to have a more request

1. I will run this script three times a day , so user will receive the same error statement six times even they have fixed the error , I would like if the error statement has already sent to the user , then it will NOT send the same error statement again , that means user will receive the same error statement one time only ;

2. if no error then don't send the mail to user

can advise what can I do ? Thx in advance.
Peter Nikitka
Honored Contributor

Re: script wanted

Hi,

regarding 2)
I gave already a solution:
...
done >$TMP
if [ -s $TMP ]
then
mailx -s "shutdown requests" youraddr@somewhere.net <$TMP
fi
...

reagarding 1)
Simplest solution: keep the last result and compare against this:
...
done >$TMP
if [ -s $TMP ]
then
if [ -s $TMP.last ] && cmp -s $TMP $TMP.lst
then rm $TMP
else
mailx -s "shutdown requests" youraddr@somewhere.net <$TMP
mv $TMP $TMP.last
fi
else rm $TMP
fi

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"