Article Listing 1 Listing 2 Listing 3 Listing 4
Listing 5 Listing 6 Listing 7 Listing 8 Listing 9
Listing 10 Listing 11 Listing 12 dec2004.tar

Listing 10 count_added.sh

#!/bin/ksh

#***********************************************************************
# Listing 10:
# File: count_added.sh
#
# Description:
#
# This script takes a file generated by the compare command, and 
# counts the number of files that were added. 
#
# A file is added if its pathname appeared in the right-hand-side file 

# but not in the left-hand-side file when the comparison is made.  If a 
# file is added, the comparison file will contain a line that begins 
# with > and contains the file's pathname in field 2.  Also, if a file
# is added, it will not contain any lines that begin with < and
# contain the file's pathname in field 2.
#
# Author: John Spurgeon (john.p.spurgeon@intel.com)
#
#***********************************************************************

if [ "$#" -ne 1 ]
then
   echo "[$(basename $0)] Usage: $(basename $0) filename"
   exit 0
fi

# FILE is the name of a file generated by a previous comparison.
# The format must be consistent with the output produced by the compare command.
FILE=$1

if ! [ -f $FILE ]
then
   echo "[$(basename $0)] Error: $FILE does not exist!" >& 2
   exit 1
fi

# Count the total number of lines in the file.
TOTAL=$(wc -l < $FILE)

# Count the number of unique file names. 
UNIQUE=$(awk '{print $2}' $FILE | sort -u | wc -l | awk '{print $1}')

# Count the number of file name's that appeared in the rhs file.
RHS=$(awk 'BEGIN { n = 0 } $1 == ">" { n++ } END {print n}' $FILE)

# Calculate the number of filenames that appeared in both the rhs and lhs files.
# This is the number of files that have been modified or "changed".
CHANGED=$((TOTAL - UNIQUE))

# Calculated the number of filenames that appeared ONLY in the rhs file.
# This is the number of files that were "added". 
echo $((RHS - CHANGED))


#!/bin/ksh