Solaris Tip: Have Your Files Changed Since Installation?

By  

Everything beyond the file size goes into $misc.

3) A little further down, we check the file type as stored in the contents file. This determined what kind of information is reported by pkgchk. For example, pkgchk won't report on file sizes for files whose sizes are expected to change.

4) We then run through a series of comparisons between the original and current settings. For checking permissions, we grab the string (e.g., 0444) from the pkgchk output and use them as an argument to chmod on a temporary file only so we can easily compare -r--r--r-- type strings.

5) If you prefer no output when the file parameters match, you can simply omit the else portions of the if commands following the "Now, check current settings against the original settings" comment.

#!/bin/bash

if [ $# == 0 ]; then
    echo -n "file to check> "
    read FILE
else
    FILE=$1
fi

# Make sure we have the full path
while [ ! -f $FILE ]
do
    if [ `echo $FILE | cut -c1` != "/" ]; then
        echo -n "Please enter file name with full path> "
        read FILE
    else
        echo "No such file: $FILE"
        exit 1
    fi
done

# Get file stats
ls -l $FILE > /tmp/stats$$
read mode links owner group size misc < /tmp/stats$$

# Get pkgchk output
pkgchk -l -p $FILE > /tmp/check$$

# Get file type
Type=`grep -w $FILE /var/sadm/install/contents | awk '{print $2}'`
if [ "$Type" = "" ]; then
    echo "$FILE not found in the contents file"
    exit 2
fi

# Get current file stats
ls -l $FILE > /tmp/stats$$
read mode links owner group etc < /tmp/stats$$

# Now, check current settings against the original settings

# compare owner/group
OrigOwner=`grep owner /tmp/check$$ | awk '{print $NF}'`
OrigGroup=`grep group /tmp/check$$ | awk '{print $NF}'`

if [ $owner != $OrigOwner ]; then
    echo "File owners do not match"
else
    echo owner ok
fi
if [ $group != $OrigGroup ]; then
    echo "File groups do not match"
else
    echo group ok
fi

# Compare size
if [ $Type == "f" ]; then
    CurrSize=`ls -l $FILE | awk '{print $5}'`
    if [ $CurrSize != $size ]; then
        echo "File size does not match"
    else
        echo File size ok
    fi
fi

# Compare permissions
touch /tmp/junk$$
OrigMode=`grep mode: /tmp/check$$ | awk '{print $NF}'`
chmod $OrigMode /tmp/junk$$
omode=`ls -l /tmp/junk$$ | cut -c1-10`

if [ $mode != $omode ]; then
    echo "File permissions do not match"
else
    echo "File permissions ok"
fi

# Compare sums
if [ $Type == "f" ]; then
    CurrSum=`sum $FILE |  awk '{print $1}'`
    OrigSum=`grep sum /tmp/check$$ | awk '{print $NF}'`
    if [ $CurrSum != $OrigSum ]; then
        echo "File sums do not match"
    else
        echo "File sum ok"
    fi
fi

# clean up
rm /tmp/check$$
rm /tmp/stats$$
rm /tmp/junk$$
Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Answers - Powered by ITworld

ITworld Answers helps you solve problems and share expertise. Ask a question or take a crack at answering the new questions below.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Ask a Question