Help understanding the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help understanding the script
# 8  
Old 10-13-2016
I am using AIX 7.1 and it looks like I do not have the GNU date, any other suggestion to simplify this script?
# 9  
Old 10-13-2016
Quote:
Originally Posted by hasn318
I am using AIX 7.1
This is interesting because under this circumstances the script should not run: in AIX 7.1 the default shell is a ksh88 and as far as i know these substring-functions ${var:offset:length} are only available in ksh93, which resides in /usr/bin/ksh93, not /usr/bin/ksh. You might want to investigate.

I hope this helps.

bakunin
# 10  
Old 10-13-2016
Quote:
Originally Posted by hasn318
I am using AIX 7.1 and it looks like I do not have the GNU date, any other suggestion to simplify this script?
It's a fine line between what you have and using perl to do the date processing. As we don't have the whole script some assumptions need to be made on what you are trying to do with these Julian values.

It appears this script is trying to calculate the age in days of particular past dates. The problem with the solution used in this script is that leap years will cause 1 day errors.

Under AIX I would propose using a perl script that tells you the age (in days) of a date eg:

Code:
$ agedays.pl "Jan 20 2016"
268

agedays.pl could be written like this:

Code:
#!/bin/perl
use POSIX;
use Time::Local;

my %mon;
@mon{qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/} = 0..11;

my ($mmm, $dd, $yy) = split(/[-\/ ]/, $ARGV[0]);

printf "%.0f", (time() - timelocal(0,0,0,$dd,$mon{$mmm}, $yy) - 0.4999) / (24*60*60);


The script then could forget about Current and just fetch the age of the backup like this:

Code:
PrevHostname=''
PrevBackup=0
while read input
do
  if [[ ${input:9:1} = ':' ]] then
     DateStr="${input:0:3} ${input:4:2} $(date +%Y)"
  else
     DateStr="${input:0:3} ${input:4:2} 20${input:10:2}"
  fi
  BackupAge=$(agedays.pl "$DateStr")
  ...

# 11  
Old 10-14-2016
Below is the full script

Code:
#!/usr/bin/ksh
#
#   mksysb.ksh      verifies that current mksysb images are being maintained by the two mksysb (NIM) hosts,
#                   one in the DC1 and anohter in the DC2.   The program reads the available mksysb images
#                   from both hosts along with the date they were created.   Converts the creation time into
#                   a quasi-julian calendar format for comparision the current date.   Any mksysb image less
#                   than three days old is considered "current".   Older images are considered "stale".   Any
#                   system belonging to the Unix Operation team from which a mksysb can not be found is
#                   considered "missing".   
#
# while true; do                               # end-less loop - can be removed in favor of a cron entry
Month=`date|cut -c5-7`                         # break current date - "Wed Sep 23 16:33:34 EDT 2015"
Day=`date|cut -c9-10`                          # into Month(Mmm), Day(##) & Year(##)
Year=`date|cut -c27-28`
Rom2Jul() { case $Month in Feb) Day=$(( $Day+31 ));;
                           Mar) Day=$(( $Day+59 ));;
                           Apr) Day=$(( $Day+90 ));;
                           May) Day=$(( $Day+120 ));;
                           Jun) Day=$(( $Day+151 ));;
                           Jul) Day=$(( $Day+181 ));;
                           Aug) Day=$(( $Day+212 ));;
                           Sep) Day=$(( $Day+243 ));;
                           Oct) Day=$(( $Day+273 ));;
                           Nov) Day=$(( $Day+304 ));;
                           Dec) Day=$(( $Day+334 ));; esac }
Rom2Jul
Current=$(( $Year*365+$Day-2 ))                # current ddddd less 3 days to allow for delayed backups & leap year
Path=/tmp/mksysb                             # for testing ONLY!
BackupInput=$Path/mksysbChk.input
ContactNIM=/usr/local/admin/bin/remote.pl
$ContactNIM --host=NIM1 --command=/bin/'ls -al /nim/dr/mksysb/\*'|grep -e _dr -e .tgz|cut -c37-90|cut -d" " -f2-8|cut -d_ -f1 > $BackupInput     # place DC1 clients into the list
$ContactNIM --host=NIM2 --command=/bin/'ls -al /nim/dr/mksysb/\*'|grep -e _dr -e .tgz|cut -c37-90|cut -d" " -f2-8|cut -d_ -f1 >> $BackupInput   # append DC2 clients into the list
BackupMsg=$Path/mksysbChk.msg                  # setup mail strings
BackupTitle='mksysb Status Report'
BackupAdm='xxxxx@xxxx.com'
if [[ ! -s $BackupInput ]] then BackupMsg='ERROR - unable to communicate with the NIM servers and verify mksysb status - NO notifications sent'
                                mail -s "$BackupTitle" $BackupAdm < $BackupMsg; exit; fi   # connection to NIM servers lost, aborting!!!
BackupOutput=$Path/mksysbChk.output
if [[ -e $BackupOutput ]] then rm $BackupOutput; fi
PrevHostname=''
PrevBackup=0
while read input                               # eliminate duplicate mksysbChk.files & convert raw dates into yy x 365 + ddd format
do Month=${input:0:3}
   Day=${input:4:2}
   Hostname=${input:13:12}
   Rom2Jul
   if [[ ${input:9:1} = ':' ]] then Year=`date|cut -c27-28`; else Year=${input:10:2}; fi
   Backup=$(( $Year*365+$Day ))
   if [[ $PrevHostname = '' ]] then PrevHostname=$Hostname; PrevBackup=$Backup
      else if [[ $PrevHostname != $Hostname ]] then echo $PrevBackup $PrevHostname >> $BackupOutput
                                                    PrevHostname=$Hostname; PrevBackup=$Backup
              else if (( $Backup > $PrevBackup )) then PrevBackup=$Backup; fi; fi; fi; done < $BackupInput
echo $PrevBackup $PrevHostname >> $BackupOutput   # write out final record
if [[ -e $BackupMsg ]] then rm $BackupMsg; fi
SupportedHosts=/tmp/aix.lst                    # daily extract of the supported hostnames
while read Hostname                            # match supported hostnames with mksysbChk.files & determine status (current/stale/missing)
do Backup=`grep $Hostname $BackupOutput|cut -c1-5`   # actual match
   if [[ $Backup == '' ]] then echo "$Hostname\tMissing\tmksysb" >> $BackupMsg
      else if [[ $Backup < $Current ]] then echo "$Hostname\tStale\tmksysb" >> $BackupMsg; fi; fi; done < $SupportedHosts
mail -s "$BackupTitle" $BackupAdm < $BackupMsg   # for testing ONLY!

# 12  
Old 10-14-2016
This should be OK for AIX - dont have system to test it on sorry.

Code:
#!/bin/ksh
#
#   mksysb.ksh      verifies that current mksysb images are being maintained by the two mksysb (NIM) hosts,
#                   one in the DC1 and anohter in the DC2.   The program reads the available mksysb images
#                   from both hosts along with the date they were created.   Converts the creation time into
#                   a quasi-julian calendar format for comparision the current date.   Any mksysb image less
#                   than three days old is considered "current".   Older images are considered "stale".   Any
#                   system belonging to the Unix Operation team from which a mksysb can not be found is
#                   considered "missing".   
#
# while true; do                               # end-less loop - can be removed in favor of a cron entry
Path=/tmp/mksysb                             # for testing ONLY!
BackupInput=$Path/mksysbChk.input
ContactNIM=/usr/local/admin/bin/remote.pl
$ContactNIM --host=NIM1 --command=/bin/'ls -al /nim/dr/mksysb/\*'|grep -e _dr -e .tgz|cut -c37-90|cut -d" " -f2-8|cut -d_ -f1 > $BackupInput     # place DC1 clients into the list
$ContactNIM --host=NIM2 --command=/bin/'ls -al /nim/dr/mksysb/\*'|grep -e _dr -e .tgz|cut -c37-90|cut -d" " -f2-8|cut -d_ -f1 >> $BackupInput   # append DC2 clients into the list
BackupMsg=$Path/mksysbChk.msg                  # setup mail strings
BackupTitle='mksysb Status Report'
BackupAdm='xxxxx@xxxx.com'

if [[ ! -s $BackupInput ]]
then
   BackupMsg='ERROR - unable to communicate with the NIM servers and verify mksysb status - NO notifications sent'
   mail -s "$BackupTitle" $BackupAdm < $BackupMsg
   exit
fi   # connection to NIM servers lost, aborting!!!

BackupOutput=$Path/mksysbChk.output

if [[ -e $BackupOutput ]]
then
   rm $BackupOutput
fi


PrevHostname=''
PrevBackup=1000

while read input
do
  if [[ ${input:9:1} = ':' ]]
  then
     DateStr="${input:0:3} ${input:4:2} $(date +%Y)"
  else
     DateStr="${input:0:3} ${input:4:2} 20${input:10:2}"
  fi
  BackupAge=$(agedays.pl "$DateStr")

 if [[ $PrevHostname = '' ]]
 then
      PrevHostname=$Hostname
      PrevBackup=$BackupAge
 else if [[ $PrevHostname != $Hostname ]]
      then
          echo $PrevBackup $PrevHostname >> $BackupOutput
          PrevHostname=$Hostname
          PrevBackup=$BackupAge
      else if (( $BackupAge < $PrevBackup ))
           then
               PrevBackup=$BackupAge
           fi
      fi
  fi
done < $BackupInput
echo $PrevBackup $PrevHostname >> $BackupOutput   # write out final record

if [[ -e $BackupMsg ]]
then
    rm $BackupMsg
fi
SupportedHosts=/tmp/aix.lst                    # daily extract of the supported hostnames
while read Hostname                            # match supported hostnames with mksysbChk.files & determine status (current/stale/missing)
do BackupAge=`grep $Hostname $BackupOutput|tail -1 | cut -d' ' -f 1`   # actual match
   if [[ $BackupAge == '' ]]
   then
       echo "$Hostname\tMissing\tmksysb" >> $BackupMsg
   else if [[ $BackupAge > 0 ]]
        then
           echo "$Hostname\tStale\tmksysb" >> $BackupMsg
        fi
   fi
done < $SupportedHosts
mail -s "$BackupTitle" $BackupAdm < $BackupMsg   # for testing ONLY!


Note in your script I doubt the line below would work:

Code:
do Backup=`grep $Hostname $BackupOutput|cut -c1-5`   # actual match

Firstly the $BackupOutput file would contain multiple entries for the host as it's appended each time and also the cut of the first 5 characters would not fetch the numeric julian days correctly. I've replaced this with tail -1 to get the last (most recent) entry for host and used -f option on cut to fetch the first file (being the age).
# 13  
Old 10-14-2016
I am getting the below error, do I need to install a Perl module?

Code:
agedays.pl: not found [No such file or directory]

I do have Perl

Code:
which perl
/usr/local/bin/perl

# 14  
Old 10-14-2016
Have a look at post 10. There is a suggestion for writing your own perldays.pl in there.



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Understanding a script for sum

Hello, How come the following script adds each numeric value to a total sum ? x=$1 func() { for i in $1 $2 $3; do let x= $x+$i done } func "8 8 8" 9 9 echo $x A.How the program sums the string "8 8 8" if it`s only the first field value ($1)? B.If we define x to be $1... (3 Replies)
Discussion started by: uniran
3 Replies

2. UNIX for Beginners Questions & Answers

Shall script use and understanding

Below script is called as Reducer, I am not sure how it work, can some expert explain me what this script does as i am a beginner. inputfile: hi hi how are are you code: #!/bin/bash lastkey=""; -- what does this mean, because i saw in debug mode it is taking value as hi count=0;... (13 Replies)
Discussion started by: mirwasim
13 Replies

3. Shell Programming and Scripting

Help--understanding the script

Hi, New to this forum, as well as to unix scripting..need help to understand below script ... sendNotice_sp() { ATTACH=${LNXLOG:-} if ; then if ; then mail -s "$ERR_MSG" $ERR_EMAIL_TO < $ATTACH fi else mail -s "$ERR_MSG" $ERR_EMAIL_TO < /dev/null fi } afaik this is sm kind of... (6 Replies)
Discussion started by: gnnsprapa
6 Replies

4. Shell Programming and Scripting

Help understanding the script

Hi Guys, I am new to scripting , I am trying to rebuild a script based on an old script. Can someone help me figure out what the script is doing? This is only a part of the script. I am looking to interpret these two points in the scripts:- 1) test=`echo $?` while I do not... (3 Replies)
Discussion started by: rajsan
3 Replies

5. UNIX for Dummies Questions & Answers

help with understanding script

i am trying to understand this script example. the text does not explain it. can someone tell me briefly what each of the functions do. any help will be appreciated. #!/bin/bash killtree() { local _pid=$1 local _sig=${2-TERM} for _child in $(ps -o pid --no-headers --ppid... (1 Reply)
Discussion started by: iluvsushi
1 Replies

6. Shell Programming and Scripting

Need help understanding this script.

Can someone explain what is happening line by line in this script, particularly after the do statement. The script works, it renames all the files in my directory that has a date in the file name. But I would like to know more about it. #!/bin/bash newdate=12-10-1995 for file in *--* do ... (6 Replies)
Discussion started by: Harleyrci
6 Replies

7. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

8. Shell Programming and Scripting

Need help for understanding of script

# sub: find block (in cols), return line-numbers (begin-end) or 0 if notfound sub findb{ my ($exp1,$col1,$exp2,$col2)= @_; # $exp = expression to find, $col - column to search in my $cnt=0; my ($val1,$val2); my ($beg,$end); for($cnt=1;$cnt<=65536;$cnt++){ $val1 =... (3 Replies)
Discussion started by: suvenduperl
3 Replies

9. Shell Programming and Scripting

Help understanding a script

Hello everybody, Can anybody tell me of what "~" refers to in the below code snippet. lsvg $vgNAME | awk 'BEGIN {freeDISK=1} {if (($4 ~ /PP/ && $5 ~ /SIZE/) || ($4 ~ /FREE/ && $5 ~ /PPs/)) {freeDISK *= $6 }} END {print freeDISK*1024 }' Thanks in advance, (6 Replies)
Discussion started by: tenderfoot
6 Replies

10. UNIX for Dummies Questions & Answers

Need help understanding script command

We use a UNIX-based system (Lawson) at work and I was given this command to request a data extract from the db admin. The only thing I really understand is the last line as it appears to be joining the files created from the first three lines into one. Is there anyone who can help me breakdown the... (4 Replies)
Discussion started by: KGee
4 Replies
Login or Register to Ask a Question