SHIFT

--- Sjoerd Hooft's InFormation Technology ---

User Tools

Site Tools


Sidebar

Recently Changed Pages:

View All Pages


View All Tags


LinkedIn




WIKI Disclaimer: As with most other things on the Internet, the content on this wiki is not supported. It was contributed by me and is published “as is”. It has worked for me, and might work for you.
Also note that any view or statement expressed anywhere on this site are strictly mine and not the opinions or views of my employer.


Pages with comments

View All Comments

filehandlingscript

Script: Bash: AIX: File Handling

This is a remake of a script that is designed to check consistency in files. The second line 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_ @ _warmetal.nl
#
# 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_@_warmetal.nl sjoerd_getshifting.com"
MAILTOALARMCC="ccmail1_warmetal.nl 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
You could leave a comment if you were logged in.
filehandlingscript.txt · Last modified: 2021/09/24 00:24 (external edit)