filehandlingscript
Bash: a script to check if all files are present
Summary: Bash: a script to check if all files are present
Date: 6 June 2011
Refactor: 1 March 2025: Checked links and formatting.
This is a remake of a script that is designed to check consistency in file order. The second line inside the file holds a unique number that is always 1 higher than the previous one. They also need to be on time, but only between 8 and 17 during workdays. As since, this script is scheduled according so through cron.
#!/bin/bash ######################################################################################################################## # Author : Sjoerd Hooft # Date Initial Version: 6 June 2011 # Comments: sjoerd_ @ _getshifting.com # # Description: # This is the script to check consistency in files. # # Recommendations: # The script is designed for a 120 column terminal. # The running user must be the root user # # Changes: # Please comment on your changes to the script (your name and email address, line number, description): ######################################################################################################################## # Script Variables HOSTNAME_SHORT=`hostname -s` BASEDIR=`dirname $0` LOGFILE="$BASEDIR/file.log" WHATAMI=`basename $0` DATE=`date +%d%m%y` # Send all output to logfile exec > $LOGFILE 2>&1 # Mail Variables . "$BASEDIR/mail.txt" MAILTOALARM="alarm_@_getshifting.com sjoerd_getshifting.com" MAILTOALARMCC="ccmail1_getshifting.com ccmail2_getshifting.com" # File Variables FILEDIR="/DIR/files" TEMPSEQFILE="./seqnr.tmp" mailFunction() { if [[ "$failstatus" -eq "1" ]]; then cat $LOGFILE | mail -s "Possible failure files" -c $MAILTOALARMCC $MAILTOALARM else cat $LOGFILE | mail -s "File report" $MAILTOSUCCESS fi } # Determine file sequence number # Needs input of file to check fileseqnrFunction() { LINE_COUNT=0 while read LINE do # Need to skip first line, as sequence number is on second line if [[ $LINE_COUNT -eq 1 ]] ; then # Set variable and cut the ^m (new line) from the line FILESEQNR=`echo $LINE | cut -f1 -d" "` echo "The sequence number of $(echo $1 | cut -f5 -d"/") = $FILESEQNR" echo echo $FILESEQNR >> $TEMPSEQFILE break fi LINE_COUNT=$(($LINE_COUNT+1)) done < $1 } # Determine all file today and set them into an array todaysfilesFunction() { declare -a FILEARRAY FILEARRAY=($(find $FILEDIR/. -name "${DATE}*" -print)) for file in "${FILEARRAY[@]}" do echo "Checking this todays file: $file" fileseqnrFunction $file done } # Checking for missing sequence numbers sequenceFunction() { declare -a SEQNRARRAY SEQNRARRAY=($(cat $TEMPSEQFILE | sort)) echo "There are ${#SEQNRARRAY[@]} files so far today that need to be checked." echo lastelement=${#SEQNRARRAY[@]} #uncomment for troubleshooting echo "So the array exists from elements ${SEQNRARRAY[0]} to ${SEQNRARRAY[@]: -1} " while [ "$lastelement" != "1" ] do # Array is zero indexed, so start with retracting 1 secondtolastelement=$(($lastelement - 2)) lastelement=$(($lastelement - 1)) check=$((${SEQNRARRAY[$lastelement]}-${SEQNRARRAY[$secondtolastelement]})) if [ "$check" != "1" ]; then echo "The difference between ${SEQNRARRAY[$lastelement]} and ${SEQNRARRAY[$secondtolastelement]} is $check. There is a file missing. Oh oh... " failstatus=1 else echo "The difference between ${SEQNRARRAY[$lastelement]} and ${SEQNRARRAY[$secondtolastelement]} is $check. Nice. There are no files missing." fi done } timeFunction() { hour="10#`date +%H`" # find must be non recursive on AIX, use prune and * see [[scriptbashfind#using_prune]] find=`find $FILEDIR/* -prune -type f -mmin -120 -print | wc -l` echo "This is the report from `date +%H` hour and there are $find files in the last two hours. " # if time before 10 AM and there are no files if [[ "$hour" -lt "10" ]] && [[ "$find" -le "1" ]]; then echo "There are not enough files yet to check for sequence numbers but it's still early. We'll check again next hour." mailFunction exit fi if [[ "$hour" -ge "10" ]] && [[ "$find" -eq "0" ]]; then echo ls -lrt $FILEDIR | tail echo "Oh oh, the latest file is already more than two hours old. Is there something wrong? " failstatus=1 else echo "Latest file is recent enough. Very well." echo fi } timeFunction todaysfilesFunction sequenceFunction rm $TEMPSEQFILE mailFunction exit
filehandlingscript.txt · Last modified: by 127.0.0.1