#!/bin/bash # compare-common: Verifies files in a directory against a # designated template file (called by compare-dirs) # Copyright (C) 2009 David Leutzinger (leutzdave @ charter.net) # /home/david/bin/Website/compare/compare-common # 2009-08-02 vs 0.2 # 2009-08-11 vs 0.3 # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # ----- FUNCTIONS START ----- make_compare_file () { # Define and log the output filename #echo "make_compare_file function running ..." | tee -a $log_file echo "source_file = $source_file" >> $log_file filename=`echo $source_file | rev | cut -d"." -f"2-" | cut -d"/" -f"1" | rev` compare_file_1A="$log_dir/$filename-begin.txt" compare_file_1B="$log_dir/$filename-end.txt" echo "compare_file_1A = $compare_file_1A" >> $log_file echo "compare_file_1B = $compare_file_1B" >> $log_file content_file_1="$log_dir/$filename-content.txt" echo "content_file_1 = $content_file_1" >> $log_file # Determine and log cut_line numbers in source file # -n, --line-number # Prefix each line of output with the 1-based line number within its input file. cut_line_1=`grep -n "\-\- CONTENT \-\-" $source_file | cut -d":" -f"1"` cut_line_2=`grep -n "\-\- FOOTER \-\-" $source_file | cut -d":" -f"1"` echo "cut_line_1 = $cut_line_1" >> $log_file echo "cut_line_2 = $cut_line_2" >> $log_file # Determine and log total lines in source file lines_total=`wc -l $source_file | cut -d" " -f"1"` echo "lines_total = $lines_total" >> $log_file # Calculate number of lines for text grabs head_lines=`expr $cut_line_1 - 1` tail_lines=`expr $lines_total - $cut_line_2 + 1` echo "head_lines = $head_lines" >> $log_file echo "tail_lines = $tail_lines" >> $log_file # Grab the text for comparison file head -n $head_lines $source_file > $compare_file_1A tail -n $tail_lines $source_file > $compare_file_1B # Number the lines of the compare files nl --body-numbering=a --number-format=rn "$compare_file_1A" > "$log_dir/temp.txt" mv "$log_dir/temp.txt" "$compare_file_1A" nl --body-numbering=a --number-format=rn "$compare_file_1B" > "$log_dir/temp.txt" mv "$log_dir/temp.txt" "$compare_file_1B" # Grab the content section also lines_center_1=`expr $cut_line_2 - 1` lines_center_2=`expr $cut_line_2 - $cut_line_1` echo "lines_center_1 = $lines_center_1" >> $log_file echo "lines_center_2 = $lines_center_2" >> $log_file head -n $lines_center_1 $source_file | tail -n $lines_center_2 > $content_file_1 # Number the lines of the content file nl --body-numbering=a --number-format=rn "$content_file_1" > "$log_dir/temp.txt" mv "$log_dir/temp.txt" "$content_file_1" #echo "make_compare_file function completed." | tee -a $log_file } # ----- FUNCTIONS END ----- # ----- MAIN START ----- # These are passed from the compare_dir menu program DEV_DIR="$1" COMPARE_DIR="$2" STANDARD_FILE="$3" # NO changes required below this line # Setup logging directory and log_file log_dir="/home/david/bin/Website/compare/$COMPARE_DIR" log_file="$log_dir/Compare.log" # Clear last logs and start new log rm $log_dir/* date=`date` echo "Run for $DEV_DIR/$COMPARE_DIR started at $date ..." | tee $log_file # Set-up for and run make_compare_file function source_file="$DEV_DIR/$COMPARE_DIR/$STANDARD_FILE" echo "" | tee -a $log_file echo "PRE-LOOP - Base Compare File = $DEV_DIR/$COMPARE_DIR/$STANDARD_FILE" | tee -a $log_file make_compare_file # Save the resultant compare_file name file_1A="$compare_file_1A" echo "file_1A = $compare_file_1A" | tee -a $log_file file_1B="$compare_file_1B" echo "file_1B = $compare_file_1B" | tee -a $log_file file_1C="$content_file_1" echo "file_1C = $content_file_1" | tee -a $log_file # PAUSE for diagnostic purposes #read pause loop_cntr="0" for i in `ls -1 $DEV_DIR/$COMPARE_DIR/*.html | sort` do loop_cntr=`expr $loop_cntr + 1` echo "" | tee -a $log_file echo "loop_cntr = $loop_cntr - Processing File $i" | tee -a $log_file if [ "$i" = "$DEV_DIR/$COMPARE_DIR/$STANDARD_FILE" ];then echo "file_1A = $file_1A (already generated)" | tee -a $log_file echo "file_1B = $file_1B (already generated)" | tee -a $log_file echo "file_1C = $file_1C (already generated)" | tee -a $log_file else # Set-up for and run make_compare_file function source_file="$i" make_compare_file # Save the resultant compare_file name file_2A="$compare_file_1A" echo "file_2A = $compare_file_1A" | tee -a $log_file file_2B="$compare_file_1B" echo "file_2B = $compare_file_1B" | tee -a $log_file file_2C="$content_file_1" echo "file_2C = $content_file_1" | tee -a $log_file # Perform the compare and log it # -b --ignore-space-change # Ignore changes in the amount of white space. # -w --ignore-all-space # Ignore all white space. # -B --ignore-blank-lines # Ignore changes whose lines are all blank. # -y --side-by-side # Output in two columns. diff "$file_1A" "$file_2A" > "$log_dir/diff-$filename-begin.txt" echo "DIFF = $log_dir/diff-$filename-begin.txt" | tee -a $log_file diff "$file_1B" "$file_2B" > "$log_dir/diff-$filename-end.txt" echo "DIFF = $log_dir/diff-$filename-end.txt" | tee -a $log_file # Note that content files are not compared fi # PAUSE for diagnostic purposes #read pause done # Close out the log date=`date` echo "Run ended at $date ..." | tee -a $log_file # ----- MAIN END -----