AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
 View Only
  • 1.  in KSH, how to correctly evaluate combined vars

    Posted Tue December 30, 2008 09:00 PM

    Originally posted by: SystemAdmin


    Hi, I am a intermediate programmer, experienced in Java, REXX, and other script languages, and am new to KSH. I am trying to combine two variables to evaluate to the combination, but it seems I may have a) poor syntax, or b) wrong code structure.
    I've researched this in many places and read through documentation, but am convinced that the answer lies in "experience". To me, this seems to be a very rudimentary question, yet try as I might, just about every combination I can think, I've yet to achieve the correct results.

    So here's the scenario:

    I created (and exported) some environmental variables within my .profile. They are $ENV1, $ENV2, $ENV3 and $VARLINES. These evaluate correctly when performing an echo of the vars.

    I then try to combine the "$ENV" with a generated number "$snum" and would like these var to evaluate properly.

    In the code snippet attached, line 15 is the line I need evaluated properly
    e.g. SERVERS$snum=$ENV$snum needs to evaluate to SERVERS[1] = $ENV1
    SERVERS[2] = $ENV2 and finally SERVERS[3] = $ENV3

    Attached is a code snippet to demonstrate.
    Thank you in advance
    #AIX-Forum


  • 2.  Re: in KSH, how to correctly evaluate combined vars

    Posted Wed December 31, 2008 09:44 AM

    Originally posted by: esv


    here is how I did it...
    set -A SERVERS
    ENV1=1
    ENV2=2
    ENV3=3

    for i in 1 2 3
    do
    let SERVERS$i=ENV$i
    done

    for i in 1 2 3
    do
    echo ${SERVERS$i}
    done

    best regards & happy new year.
    esv
    #AIX-Forum


  • 3.  Re: in KSH, how to correctly evaluate combined vars

    Posted Wed December 31, 2008 09:53 AM
      |   view attached

    Originally posted by: esv


    darn, it does not paste well. here it is once more, I've attached a text file with the right syntax.

    best regards,
    esv.
    
    set -A SERVERS ENV1=1 ENV2=2 ENV3=3 
    
    for i in 1 2 3 
    
    do let SERVERS[$i]=ENV$i done   
    
    for i in 1 2 3 
    
    do echo $
    {SERVERS[$i]
    } done
    

    #AIX-Forum

    Attachment(s)

    txt
    attachment_14180158_dw.txt   175 B 1 version


  • 4.  Re: in KSH, how to correctly evaluate combined vars

    Posted Wed December 31, 2008 12:26 PM
      |   view attached

    Originally posted by: SystemAdmin


    Thank you Enrique

    This is close to what I need.

    The format of the displayed variable "SERVERS$i=ENV$i" works correct.

    The difficulty right now seems to be properly evaluating the right side of the set command.

    I cannot ever get the Environment values developed in .profile to load in this array.
    The passed vars are text values:
    e.g. $ENV1="LSR"
    $ENV2="LENDB"
    $ENV3="LNOTES"

    So, trying a substitution in your code as ENV1=$ENV1, ENV2=$ENV2, ENV3=$ENV3 e.t.c fails with:

    test4.sh[7]: LSR: bad number
    test4.sh[7]: LENDB: bad number
    test4.sh[7]: LNOTES: bad number

    Also, because the number of passed ENV vars is variable I cycle though a loop (and That works fine)
    so it's just getting the value of $ENV1, $ENV2 e.t.c into the SERVERS$i array that still fails.

    I may just have to resort to reading the input file again as .profile does, but did not want to do this.
    #AIX-Forum

    Attachment(s)



  • 5.  Re: in KSH, how to correctly evaluate combined vars

    Posted Wed December 31, 2008 02:40 PM

    Originally posted by: SystemAdmin


    Ok, I've resolved to message "bad number" by removing the "let" when loading the array.

    So now, I need to be able to cycle the number for 1 to the value of LINES using substitution.
    
    LINES = 3        # This variable 
    "3" is not set here, its value is unknown until .profile reads a file # therefore the number may be 3,5,10,20 whatever. i=1 until [ $i - gt $LINES ]   
    
    do SERVERS[$i
    }]=$ENV$i   # <--- need to have the right side of 
    
    this equal iterate from $ENV1 through $ENV3     echo the servers listed so far are $
    {SERVERS[$i]
    }   ((i=i+1))   done
    

    #AIX-Forum


  • 6.  Re: in KSH, how to correctly evaluate combined vars

    Posted Fri January 02, 2009 03:20 PM

    Originally posted by: SystemAdmin


    Problem solved!

    The setting of the array with passed ENV vars has a definitive syntax.

    It is:
    
    SERVERS[$j]=$
    {env
    }
    


    The test code snippet is:

    
    j=1 set - A SERVERS 
    
    while read LINE 
    
    do firstchar=`expr substr 
    "$LINE" 1 1` 
    
    if [[ $
    {#LINE
    } > 2 ]] ; then 
    
    if [[ 
    "$firstchar" != 
    '#' ]] ; then ##  parse the variables and export echo $LINE | read env rest SERVERS[$j]=$
    {env
    } ENV[$j]=$
    {env
    } ((j=j+1)) fi fi   done < $IDFILE
    

    #AIX-Forum