Operating System - Linux
1752806 Members
5563 Online
108789 Solutions
New Discussion юеВ

Re: To pass comments on a script

 
Henry  Richards
Occasional Contributor

To pass comments on a script

Hi Guys
I have this script that runs everyday to Long list (ll in HPUX and ls -la in Solaris)all the files in all the directories.The Script when runs the nextday , captures the difference between previous day's longlist(ll) and currentday longlist (ll) using the diff command in HPUX "diff file1 file2 > file12"] and save it to new file titled "result".

The Result file looks like this

-rw-r--r-- 1 root sys 48 Sep 17 09:32 see
drwxr-xr-x 3 root sys 96 Sep 17 08:58 tde5
drwxr-xr-x 2 root sys 96 Sep 17 08:57 tse4
-rw-r--r-- 1 root sys 30 Sep 17 09:32 ree

There are 9 fields in the "result" output file.

Now I want to pass comments (like change in file owner or change in permission) for every file if there is any change in any of the fields of the file.

Can you please suggest me a way to do it and that would fit to my below mentioned script.
The script is as follows:

#! /usr/bin/sh
dt1=`date`
TODAY=`echo $dt1|cut -d" " -f3`
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`

tfile=$MONTH$TODAY$YEAR.lst
echo $tfile
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 " $tfile
ls -ltR /home/tmp >$tfile

diff $tfile $yfile >result.chg

Thanks for your help.

Thanks
Henry
5 REPLIES 5
Howard Marshall
Regular Advisor

Re: To pass comments on a script

If I understand what you are trying to do here diff may not be your best option. Diff will tell you which lines in the files that are different but it sounds to me like your goal is knowing which files in the listing are different

I know it will take more scripting but it may be easier for you to create your list files so they have the relative path name as part of the file name

Find . -exec ls -ld {} \; > listing.out

Then write your own difference scanner using grep

Cat listing.out | while read LINE
Do
FILE=$(echo $LINE | awk '{print $NF}')
OLD_LINE=$(grep $FILE oldlist.out)
If [ $LINE = $OLD_LINE]
Then
#the file has not changed
elseif [ $OLD_LINE = "" ]
then
# $FILE is new or was not in the old listing
else
#something about the file is different
#you can write something to parse it and find out exactly what
fi

of course then you will have to run through the old list file and see if there are any files in it that are not in the new listing to check for deleted files.

I think that is what I would do instead of trying to write something to interpret the diff out put. Its up to you though

H
Kent Ostby
Honored Contributor

Re: To pass comments on a script

A modification to Howard's script.

Howard has you grep the oldlisting for the line, but that will not work if you have files with extensions of another files name (e.g. files fs and fs1 .. grep "fs" would find fs1) .... sooo I'll change that to awk.


find . -exec ls -ld {} \; > listing.out

Then write your own difference scanner using grep

cat listing.out | while read LINE
do
FILE=$(echo $LINE | awk '{print $NF}')
touch myfile
rm myfile
echo "XYZZY" $FILE > myfile
cat oldlist.out>>myfile
OLD_LINE=$(awk '/XYZZY/{damatch=$2;next;}if(damatch==$NF){print $0}' myfile)
if [ $LINE = $OLD_LINE]
then
#the file has not changed
elseif [ $OLD_LINE = "" ]
then
# $FILE is new or was not in the old listing
else
#something about the file is different
#you can write something to parse it and #find out exactly what
fi
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Howard Marshall
Regular Advisor

Re: To pass comments on a script

That├в s true, and grabbing the last field won't work if you have symbolic links and I am sure there are other things wrong with my script as I didn't test it at all, just wrote it off the top of my head to point him in a possible new direction.

I was going to leave the process of debugging and case testing to him, and then that wonderful thrill of getting the thing to finally work correctly.

But thanks for pointing out that it won't run correctly as written.

Henry, the script piece I wrote will not work as written. It's just a suggested guideline for you. If you want me to actually write the script for you please send a very large check to├в ┬ж..
Hein van den Heuvel
Honored Contributor

Re: To pass comments on a script


Henry, here is a starting point... in perl.
I just could not get past the ugly, hardcoded shell data manipulations.
It just does MODE and SIZE. You do the rest!


Sample output:

$ perl x.p
Y day File Name 10112005.lst
T Day file Name 10122005.lst
10112005.lst Size Change
ps.tmp DELETED
tux.p Mode Change
x.p Size Change
10122005.lst CREATED

Script:

$ cat x.p
#
# Format and print file names (turn into subroutine?)
#
@t=localtime(time()-86400);
$yfile = sprintf ("%d%02d%04d.lst",1+@t[4],@t[3],1900+@t[5]);
@t=localtime;
$tfile = sprintf ("%d%02d%04d.lst",1+@t[4],@t[3],1900+@t[5]);
print "Y day File Name $yfile\nT Day file Name $tfile\n";
#
# Get listing for today
#
system ("ls -ltr . > $tfile");
#
# loop through result from diff, remembering some attributes for each file name.
# Hopefully file names are unique.
# Stach attribute in "T" or "Y" array based on > pr <
#
foreach (`diff $tfile $yfile`) {
($left_right, $mode, $links, $own, $group, $size, $d1, $d2, $t, $name) =split;
next unless $name;
$date = "$d1 $d2 $t";
if ($left_right eq '<') { $tmode{$name}=$mode, $tsize{$name}=$size };
if ($left_right eq '>') { $ymode{$name}=$mode, $ysize{$name}=$size };
}

#
# Walk over yesterdays names, comparing attributes.
# Delete todays files attribute when processed.
#
foreach $name (sort keys %ymode) {
$line = $name;
$line .= " Mode Change" if ($tmode{$name} ne $ymode{$name});
$line .= " Size Change" if ($tsize{$name} ne $ysize{$name});
$line = "$name DELETED" unless defined $tmode{$name};
print "$line\n";
delete $tmode{$name};
}
#
# Any remaining todays file is newly created
#
foreach $name (sort keys %tmode) {
print "$name CREATED\n";
}

Muthukumar_5
Honored Contributor

Re: To pass comments on a script

You are executing diff command without option. I have tried to help and find as,

# diff /test.log1 /test.log2 | grep -v '[0-9]*c[0-9]'
< -rw-rw-rw- 1 root sys 33 Sep 21 00:08 ./tmpfile
---
> -rw-rw-rw- 1 muthu sys 33 Sep 21 00:08 ./tmpfile


Here, owner name is changed.


You can try as,

cd

find . -type f > /tmp/log${today}.log

diff /tmp/log${today}.log /tmp/log${yday}.log | grep -v '[0-9]*c[0-9]' > result.chg

You can use my script there in,

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=961895

It will not use more calculations.

hth.



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