Operating System - Linux
1824635 Members
4228 Online
109672 Solutions
New Discussion юеВ

Shell script to run everyday using diff command

 
Henry  Richards
Occasional Contributor

Shell script to run everyday using diff command

Hi All

I am new bee to Shell scripting and HP-UX.
I am trying to write a script which should run everyday to Long list (ll in HPUX and ls -la in Solaris)all the files in all the directories except /tmp , /var directories and store in a new directory with title "Mondd" (like Sep30) .And it should also run the script nextday and capture the difference between previous day's longlist(ll) and currentday longlist (ll) [I think diff command in HPUX should help it out for Example "diff file1 file2 > file12"] and save it to new directory titled "today mondd".

I would appreciate if you guys could help me out on this.

Thanks
Henry
3 REPLIES 3
Mel Burslan
Honored Contributor

Re: Shell script to run everyday using diff command

# DIRLIST variable contains the
# list of directories you want to scan
DIRLIST="/ /home /opt /usr /data"

# ASSIGN today's date to variable TODAY
TODAY=`date +%b%d`

# find what was yesterday's date and assign
# it to the variable YESTERDAY
YESTERDAY=`cat YESTERDAYname`

# scan the directories and build today's file
for DIR in $DIRLIST
do
ls -al $DIR >> $TODAY
done

# run the diff command and store the output
diff $TODAY $YESTERDAY > ${TODAY}-${YESTERDAY}

# optionally delete yesterday's file
rm $YESTERDAY

# change yesterday's file name for tomorrow
echo $TODAY > YESTERDAYname


on the first day, just run this portion to build the first day's file as you do not have anything to run the diff command on.

DIRLIST="/ /home /opt /usr /data"
TODAY=`date +%b%d`
for DIR in $DIRLIST
do
ls -al $DIR >> $TODAY
done


Also adjust the path of the output files to wherever you wish to store them, otherwise this code above will place them where you launch this code from, i.e., current directory.

Hope this helps

________________________________
UNIX because I majored in cryptology...
Henry  Richards
Occasional Contributor

Re: Shell script to run everyday using diff command

#! /usr/bin/sh dt1=`date`
# year=`echo $dt1|cut -d" " -f6`
# month=`echo $dt1|cut -d" " -f2`

thisdate=`echo $dt1|cut -d" " -f3`

# fname=$month$thisdate$year.lst

# ls -R >$fname

set -A DAYS Sat Sun Mon Tue Wed Thu Fri Sat
set -A MONTHS Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
YESTERDAY=$((`date +%d` -1))
MONTH=`date +%m`
YEAR=`date +%Y`
NDAY=`date +%u`

fname=$MONTH$thisdate$YEAR.lst

echo $fname
WEEKDAY=${DAYS[`date +%u`]}

if [ $YESTERDAY -eq "0" ]
then MONTH=$((MONTH-1)) if [ $MONTH -eq "0" ]
then
MONTH=12 YEAR=$((YEAR-1))
fi
set `cal $MONTH $YEAR`
shift $(($# - 1))
YESTERDAY=$1
fi
TMONTH=${MONTHS[MONTH]}

yfile=$MONTH$YESTERDAY$YEAR.lst

echo "Y day File ^Name " $yfile
echo "T Day file name " $fname
ls -R >$fname
diff $fname $yfile >result.lst

Can you guys Please help me on how to improve on this.I would also like to aexlude /home & /Var

Please suggest how to improve it better

Thanks
Henry
Muthukumar_5
Honored Contributor

Re: Shell script to run everyday using diff command

You can use this script:

#!/bin/ksh
# diffscr.ksh

LOGFILE=/var/diff/$(date +'%b%d').log

# Prev. Date
OLDTZ=$TZ
TZ=$TZ+24
YESLOGFILE=/var/diff/$(date +'%b%d').log
TZ=$OLDTZ

# File & Directory Search - Add more if you want
for fdir `ls -l | grep -Ev 'var|tmp|home|total' | awk '{ print $NF }'`
do

find $fdir -type f -exec ls -l {} \+ >>$LOGFILE

done

if [ -f $LOGFILE && -f $YESLOGFILE ]
then

diff $LOGFILE $YESLOGFILE

fi

exit 0
# END

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