1753868 Members
7126 Online
108809 Solutions
New Discussion юеВ

shell script check

 
SOLVED
Go to solution
rhansen
Frequent Advisor

shell script check

Hello,
The script below is finding orphan files as designed. The only change I need to add is to send me an email only if the system has orphan files right now it sends me an email whether it find an orphan file or not.

#!/usr/bin/sh
typeset -r OSNAME=$( uname -s )
typeset -l HOSTNAME=$( uname -n )
typeset DSTAMP=$(date '+%m-%d-%y %H:%M:%S')
typeset MAILTO="rob.hansen@xxxxxx.com"

case "$OSNAME" in
AIX) print "OS is AIX. Checking for orphan files..."
find / -name "/proc" -prune -o \
\( -fstype jfs -o -fstype jfs2 \) -a \( -nouser -o -nogroup \) -print | xargs ls -ld
;;

SunOS) print "OS is Solaris. Checking for uorphan files..."
find / \( -nouser -o -nogroup \) | xargs ls -ld
;;
esac
cat /tmp/orphan.log | mailx -s "$HOSTNAME $DSTAMP" $MAILTO
return 0
}

Thanks in adnvance.
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: shell script check

Hi:

Redirect the output of your 'find' command into a file. Then test for the presence of a non-zero size file and send your mail only if the file contains data.

...
find / \( -nouser -o -nogroup \) | xargs ls -ld > /tmp/orphan.log
...
if [ -s /tmp/orphan.log ]; then
cat /tmp/orphan.log | mailx -s "$HOSTNAME $DSTAMP" $MAILTO
fi


Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: shell script check

Shalom,

Suggest instead of:

cat /tmp/orphan.log | mailx -s "$HOSTNAME $DSTAMP" $MAILTO

Route output to a file.

grep the file for actual orphan files and only if you find something send the email.

send=$(grep | wc -l);

if [ $send -ge 1 ]
then
cat /tmp/orphan.log | mailx -s "$HOSTNAME $DSTAMP" $MAILTO

fi
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
rhansen
Frequent Advisor

Re: shell script check

Hello,

if [ -s /tmp/orphan.log ]; then
cat /tmp/orphan.log | mailx -s "$HOSTNAME $DSTAMP" $MAILTO
fi

is working fine as expected, however I there are a few generic lines in the output with hashes as shown below:

#+#+# Date and Time: 19 Oct 2009 12:02:07
#+#+# Script: /tmp/orphan.log
#+#+# Submitted by:

Since the hashes are in every file, I need the logic to send an email only when there are files/lines besides the usual hashes.

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: shell script check

Hi:

> Since the hashes are in every file, I need the logic to send an email only when there are files/lines besides the usual hashes.

OK, you could use this:

...
FILE=/tmp/orphan.log
STUFF=$(grep -v "#+#+#" ${FILE})
if [ ! -z "${STUFF} ]; then
mailx -s "$HOSTNAME $DSTAMP" $MAILTO < ${STUFF}

Regards!

...JRF...