cc: [email protected]
Subject: Re: [ast-users] ksh93: Array question
--------

> Below is a code snippet and sample output.   This is not my current work, but 
> ju
> st a proof-of-concept to understand arrays better.  I understand that ksh 
> does n
> ot have multi-dimensional arrays, so I had to create three arrays to simulate 
> th
> at.
ksh93s does support multiple dimensional arrays.  It als support arrays
of compound variables which is more useful for this purpose.  ksh93t also
supports user define types.  Also, you code declares the arrays as
associative arrays, but your subscripts are all integers so that it can
be done with indexed arrays.

> 
> At first I explicitly declare my indexes (e.g. servers[3]="server3…") and 
> had th
> e 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 m
> e odd questions about this 6 months from now? ;)
ksh93s can output arrays of compound variables in a format that can be used
for re-input.  This means that you can store the data in a file and then
do
        . file
to read in all the data.  To store the data in a file you can do
        print -n data=
        print -v data
> 
> 
> 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 
> ca
> n not declare "set -o pipefail", I guess it did not exist that long ago.
> 
> Thanks for any insight ya'll can give.
> Eric
There have been many new features added since ksh93e.  The current version
is ksh93u.
> 

Here is you code using an array of compound variable plus a few other minor
changes:
=====  start of code =====
#!/usr/bin/env ksh93
set -o nounset -o errexit
LINE=$(printf "%.70c" =)
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}
printf "starting: %(%a, %d %b %Y, %T %Z)T\n" now
printf "UTC   : %(%Y-%m-%dT%H:%M:%SZ)T\n"  now # ISO-8601 format
print  "host  : $( hostname )"
print  "uname : $( uname -srvnp )"
print  "ksh   : ${.sh.version}"
print  ${LINE}

# ---------- ---------- ----------
# Create our arrays of made up data
# ---------- ---------- ----------
integer indx=1
typeset -a data=(
        [1]=(server=server1.example.com name=Pacific status=up)
        [2]=(server=server2.example.com name=Atlantic status=up)
        [3]=(server=server3.example.com name=Southern status=up)
        [4]=(server=server4.example.com name=Arctic status=up)
        [5]=(server=server5.example.com name=Indian status=up)
        [6]=(server=server6.example.com name=Caribbean status=down)
        [7]=(server=server8.example.com name=Bering status=up)
        [8]=(server=server9.example.com name=Persian status=up)
)

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

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

# ---------- ---------- ----------
# Loop through arrays and do some work
# ---------- ---------- ----------
integer count_up=0 count_dn=0
for     (( indx=1; indx<=max_indx; indx++ ))
do      nameref d=data[indx]
        printf "%2d - " ${indx}
        if [[ ${d.status} == 'up' ]]
        then
                printf "%s ... [ping -c 1 %s]\n" ${d.name} ${d.server}
                (( count_up += 1 ))
        else
                print "${d.name} (${d.server}) <-- offline"
                (( count_dn += 1 ))
        fi
        typeset +n d
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}
printf "finished: %(%a, %d %b %Y, %T %Z)T\n" now
print ${LINE}
exit 0
=================================

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

Reply via email to