#!/bin/bash PROGNAME=`basename $0` main() { local MATCH REGEX PART FILE ALLWHATMUSTMATCHONEOFOTHER SUBMATCH PART=header VERBOSELEVEL=2 ALLWHATMUSTMATCHONEOFOTHER=regexps LOG_FILE=${LOG_FILE:-/dev/null} while [ "X$1" != X ]; do case $1 in -b) PART=body ;; -l) ALLWHATMUSTMATCHONEOFOTHER=lines ;; *) break ;; esac shift done case $PART in header) FILE=$HEADERFILE ;; body) FILE=$BODYFILE ;; esac debug 80 "meg: PART=$PART, ALLWHATMUSTMATCHONEOFOTHER=$ALLWHATMUSTMATCHONEOFOTHER" if [ $ALLWHATMUSTMATCHONEOFOTHER = regexps ]; then # Assume all match until proven otherwise MATCH=true debug 1000 "meg: start of for loop checking if each regex can be found in file" for REGEX in "$@"; do debug 80 "meg: checking if regex \"$REGEX\" can be found in file ..." ! $EGREP_CMD -- "$REGEX" $FILE > /dev/null && { MATCH=false; debug 80 "meg: it was not found, therefore setting MATCH=$MATCH"; break; } done debug 80 "meg: end of for loop checking if each regex can be found in file" elif [ $ALLWHATMUSTMATCHONEOFOTHER = lines ]; then # Assume all lines match one of the regexps until proven otherwise MATCH=true debug 80 "meg: start of for loop checking if each line can match a regex" while IFS= read -r LINE; do debug 80 "meg: checking if line \"$LINE\" can be matched by any regex ..." # Assume each single regex does not match until proven otherwise SUBMATCH=false for REGEX in "$@"; do debug 80 "meg: checking if line \"$LINE\" matches \"$REGEX\" ..." echo -E "$LINE" | $EGREP_CMD -- "$REGEX" > /dev/null && { SUBMATCH=true; debug 80 "meg: it matches"; break; } done [ $SUBMATCH = false ] && { MATCH=false; debug 80 "meg: matching regex was not found, therefore setting MATCH=$MATCH"; break; } done < $FILE debug 80 "meg: end of for loop checking if each line can match a regex" fi debug 80 "meg: returning $MATCH ..." $MATCH } error() { [ $VERBOSELEVEL -lt 1 ] || message "$PROGNAME: ERROR: $1"; exit 1; } internal() { [ $VERBOSELEVEL -lt 0 ] || message "$PROGNAME: INTERNAL ERROR: $1" exit 2; } warning() { [ $VERBOSELEVEL -lt 2 ] || message "$PROGNAME: WARNING: $1"; } info() { [ $VERBOSELEVEL -lt 3 ] || message "$PROGNAME: INFO: $1"; } debug() { [ $VERBOSELEVEL -lt $1 ] || message "$PROGNAME: DEBUG[$1]: $2"; } message() { if [ -t 2 ]; then echo "$1" >&2 else echo "$(date '+%Y/%m/%d %H:%M:%S'): $1" >> $LOG_FILE fi return 0 } main "$@"