Hi All,
I have written the shell script which does the following :
a. Reads the *.cnf file content from the specified input folder path
b. Grep's some strings from the .cnf file and writes the output in result file(.cnf_result.txt) in output folder
c. Now, if we get new version of config files, then will put new files in input folder and run the script again
d. First script checks if there is any output files already generated, if so then it archives those files and copies to archive path
e. After archiving, it run the same grep's command onto the new file and generates the new set of result file under result path
f. Then, it check if the same file present in the archive folder if so, it compares the new result file with archived result file and generate the new *.cnf_cmp.txt file
Now, the problem is with compare result file, since am using sdiff command(does line by line comparision), it prints both old file content and as well new file content side by side
and prints the symbols ( < > | ) in between if there is any difference found.
My question is,
- Is there any better way/solution/approach/logic that we can do this
2.Can we add color codes to the lines present in the *.cnf_cmp.txt file so that, i will be easy to read/identify the changes.
like, if there is line found in old file which is not present in new, then mark that line in yellow color.. something like that.
Thanks for reading the post anyhelp is much appreciated.
Path :
/usr/config_check
-rwxr-xr-x 1 root root 4890 Feb 25 10:29 cc.sh
drwxr-xr-x 7 root root 7 Feb 25 11:01 input
drwxr-xr-x 6 root root 6 Feb 25 11:18 archive
-rw-r--r-- 1 root root 2940 Feb 25 11:18 list_files
drwxr-xr-x 2 root root 149 Feb 25 11:18 output
Main script :
root@atta # cat cc.sh
BASE_DIR=/usr/config_check
INPUT_DIR=$BASE_DIR/input/conf
ARCHIVE_DIR=$BASE_DIR/archive
OUTPUT_DIR=$BASE_DIR/output
COMPARE_RESULT=$OUTPUT_DIR/compare_result.txt
compare_config () {
OLD_DIR=`ls -lt $ARCHIVE_DIR | grep -v "total" | head -2 | tail -1 | awk '{print $9}'`
echo "******Compared result with files in $OLD_DIR******"
echo "****************************************************"
ls -l $ARCHIVE_DIR/$OLD_DIR | awk '{print $9}' > list_files
grep "result" list_files | while read result_file; do
#echo "Compare $OUTPUT_DIR/$result_file with $ARCHIVE_DIR/$OLD_DIR/$result_file"
if [ ! -z $OUTPUT_DIR/$result_file ]; then
sdiff $OUTPUT_DIR/$result_file $ARCHIVE_DIR/$OLD_DIR/$result_file >> $OUTPUT_DIR/`echo $result_file | sed 's/_result.txt//'`_cmp.txt
fi
done
}
echo "****************************************************"
echo "****************Running Config Checks Script********************"
[ -d $OUTPUT_DIR ] && mv $OUTPUT_DIR $ARCHIVE_DIR/Archive_$(date +%Y%m%d%H%M%S)
mkdir -p $OUTPUT_DIR
#find /usr/config_check/input/conf/ -name '*.new' -print | while read src
find /usr/config_check/input/conf/ -name '*.cnf' -print | while read src
do
dest=$(dirname "$src" | sed 's:.*/::')
dest="${dest}_$(basename "$src")"
#log=$(basename "$dest" .cnf)_result.txt
log=`echo $(basename "$dest") | sed 's/.new//'`_result.txt
cp "$src" "$OUTPUT_DIR/$dest"
echo "****Printing userName parameter result*********" >> "$OUTPUT_DIR/$log"
grep userName "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing hostList parameter result*********" >> "$OUTPUT_DIR/$log"
grep hostList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing appServiceName parameter result*********" >> "$OUTPUT_DIR/$log"
grep appServiceName "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing routeList parameter result*********" >> "$OUTPUT_DIR/$log"
grep routeList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing port parameter result*********" >> "$OUTPUT_DIR/$log"
grep "route\*port" "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing protocol parameter result*********" >> "$OUTPUT_DIR/$log"
grep protocol "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
echo "****Printing serverList parameter result*********" >> "$OUTPUT_DIR/$log"
grep serviceList "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "****Printing serverId parameter result*********" >> "$OUTPUT_DIR/$log"
grep serverId "$OUTPUT_DIR/$dest" >> "$OUTPUT_DIR/$log"
echo "*****************************************************" >> "$OUTPUT_DIR/$log"
done
echo "**Config Checks Script execution completed.Please check the results in output folder**"
echo "****************************************************"
#Call compare config function
compare_config