Below is a code snippet and sample output.   This is not my current work, but 
just a proof-of-concept to understand arrays better.  I understand that ksh 
does not have multi-dimensional arrays, so I had to create three arrays to 
simulate that.

At first I explicitly declare my indexes (e.g. servers[3]="server3…") and had 
the index counter incrementing inside the loop, but I found that if I comment 
out one row then it would not work.  So I moved the counter up to where I was 
making the arrays.


*  Is there a more efficient?  "better"?  method to doing this?
*  with ksh93e can I do an array of compound variables?  If so, any one have an 
example?  Can an array of compound variables be read from a data file?
*  for maintenance, does this seem readable/understandable?  Would my boss ask 
me odd questions about this 6 months from now? ;)


One of my production boxes is still on ksh93e, all the rest are at ksh93s and 
no plans to upgrade. :(  The one place where this is an issue I found is that I 
can not declare "set -o pipefail", I guess it did not exist that long ago.

Thanks for any insight ya'll can give.
Eric




=====  start of code =====

#!/usr/bin/env ksh93
set -o nounset -o errexit
SECONDS=0 # reset SECONDS timer
typeset LINE=$( printf "%070d\n" | tr '0' '=' ) # line of double dashes
export LC_ALL=en_US.UTF-8
integer XTRACE=${XTRACE:=0} # turn on "export XTRACE=1"
                            # turn off "unset XTRACE"
(( ${XTRACE} )) && { print -u2 'DEBUG ***> Turning on xtrace'; set -o xtrace -o 
verbose; }

# ---------- ---------- ----------
print ${LINE}
print "starting : $( date "+%a, %d %b %Y, %T %Z" )"
print "UTC   : $( date -u "+%Y-%m-%dT%H:%M:%SZ" ) "# ISO-8601 format
print "host  : $( hostname )"
print "uname : $( uname -srvnp )"
print "ksh   : ${.sh.version}"
print ${LINE}

# ---------- ---------- ----------
# Create our arrays of made up data
# ---------- ---------- ----------
set -A servers
set -A names
set -A statuses
integer indx=1
servers[${indx}]='server1.example.com'; names[${indx}]='Pacific';   
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server2.example.com'; names[${indx}]='Atlantic';  
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server3.example.com'; names[${indx}]='Southern';  
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server4.example.com'; names[${indx}]='Arctic';    
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server5.example.com'; names[${indx}]='Indian';    
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server6.example.com'; names[${indx}]='Caribbean'; 
statuses[${indx}]='down'; (( indx += 1 ))

# missing # servers[${indx}]='server7.example.com'; names[${indx}]='Med'; 
statuses[${indx}]='up'; (( indx += 1 ))

servers[${indx}]='server8.example.com'; names[${indx}]='Bering';    
statuses[${indx}]='up'; (( indx += 1 ))
servers[${indx}]='server9.example.com'; names[${indx}]='Persian';   
statuses[${indx}]='up'; (( indx += 1 ))

# ---------- ---------- ----------
integer max_indx
(( max_indx = indx - 1 ))

# ---------- ---------- ----------
print "\tServer Count: ${#servers[*]}"
print ${LINE}

# ---------- ---------- ----------
# Loop through arrays and do some work
# ---------- ---------- ----------
(( indx = 1 ))
integer count_up=0
integer count_dn=0
while (( indx <= max_indx ))
do

  typeset server=${servers[${indx}]}
  typeset name=${names[${indx}]}
  typeset status=${statuses[${indx}]}

  printf "%2d - " ${indx}
  if [[ ${status} == 'up' ]]
  then

    printf "%s ... [ping -c 1 %s]\n" ${name} ${server}
    (( count_up += 1 ))

  else

    print "${name} (${server}) <-- offline"
    (( count_dn += 1 ))

  fi
  (( indx = indx + 1 ))

done
print ${LINE}
print "\t${count_up} server$( (( count_up == 1 )) && print ' ' || print 's ' 
)up"
print "\t${count_dn} server$( (( count_dn == 1 )) && print ' ' || print 's ' 
)down"
print ${LINE}
print "finished : $( date "+%a, %d %b %Y, %T %Z" )"
print ${LINE}
exit 0

===== end of code ======





output:


======================================================================
starting : Thu, 10 Mar 2011, 13:01:27 PST
UTC   : 2011-03-10T21:01:27Z
host  : xxxxx
uname : AIX xxxxx 3 5 powerpc
ksh   : Version M-12/28/93e
======================================================================
        Server Count: 8
======================================================================
 1 - Pacific ... [ping -c 1 server1.example.com]
 2 - Atlantic ... [ping -c 1 server2.example.com]
 3 - Southern ... [ping -c 1 server3.example.com]
 4 - Arctic ... [ping -c 1 server4.example.com]
 5 - Indian ... [ping -c 1 server5.example.com]
 6 - Caribbean (server6.example.com) <-- offline
 7 - Bering ... [ping -c 1 server8.example.com]
 8 - Persian ... [ping -c 1 server9.example.com]
======================================================================
        7 servers up
        1 server down
======================================================================
finished : Thu, 10 Mar 2011, 13:01:27 PST
======================================================================




_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to