#!/usr/bin/bash ################################################################################### # Script name: scanlog # # This shell script scan log files for error/warning using list of file from $1 # # example: # > scanlog # - No parameter will scan all .log files in current directory # > scanlog t_demog.log # > scanlog *.log # > scanlog t_*.log # > scanlog -f alllogs.txt # -f flag to provide list of log files in file # -h flag to display help # # Author: Sarwan Singh ################################################################################### # check for argument value if [ "${1}" = "-h" ]; then echo "Example:\n>scanlog t_demog.log" echo ">scanlog *.log" echo ">scanlog t_*.log" echo ">scanlog -f alllogs.txt" echo " -f flag to provide list of log files in file" echo " -h flag to display help" exit 0; elif [ "$1" = "-f" ]; then if [ "$2" = "" ]; then echo "\nERROR: Specify a filename with list of log files after -f." echo "For help use: scanlog -h" echo "Exiting script.\n" exit 0; else infile=${2} #stop if infile does not exit if [ ! -e $infile ]; then echo "\nERROR: '$infile' does not exist." echo "Exiting script.\n" exit 0; else allfiles=`more $infile` fi fi elif [ "$1" = "" ]; then allfiles=`ls -al *.log | grep ^- | awk '{print $9}'` else allfiles=$* fi ### Find longest filename for printng format maxlen=0; for file in ${allfiles[@]} do filelen=`echo ${#file}` if [ ${maxlen} -lt ${filelen} ]; then maxlen=$filelen fi done ### check logs for file in ${allfiles[@]} do printf "Checking: ${file}" filelen=`echo ${#file}` perl -e "print '.' x ($maxlen+5-$filelen);" if [ ! -e $file ]; then printf ":'$file' does not exist.\n" elif [ -d $file ]; then printf ":'$file' is directory.\n" elif [ -f $file ]; then checkfl=`check ${file}` if [ "$checkfl" = "" ]; then printf ":GOOD\n" else printf ":NOT GOOD\n" fi fi done