Find the answer to your Linux question:
Results 1 to 5 of 5
I have a file consisting of variable definitions like: Code: NoOfInstTypes=2 Inst.Type1="Server" Inst.Type2="Client" I want to get the value of Inst.Type variables in a loop. so, I do this Code: ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59

    getting value of a variable which consists of a variable again like ${${}}

    I have a file consisting of variable definitions like:
    Code:
    NoOfInstTypes=2
    Inst.Type1="Server"
    Inst.Type2="Client"
    I want to get the value of Inst.Type variables in a loop.
    so, I do this
    Code:
    NoOfTypes=$NoOfInstTypes
    while [ $NoOfTypes -ne 0 ]
    do
    Type=${Inst.Type$NoOfTypes}
    ..
    ..
    NoOfTypes=`expr $NoOfTypes -1`
    But this gives syntax error.

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Code:
    var1=hello
    index=1
    eval echo \$var$index
    You might also consider using an array.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    burschik's idea might work, but you'll probably also benefit by looking at corrections to your original script.

    One of them, you'll notice, is the use of underscores instead of periods.

    Hopes this helps.
    Code:
    #!/bin/bash
    
    NoOfInstTypes=2
    Inst_Type1="Server"
    Inst_Type2="Client"
    
    NoOfTypes=$NoOfInstTypes
    
    while [[ $NoOfTypes -ne 0 ]]
    do
      Type=Inst_Type$NoOfTypes
    
      echo Type is $Type
    
      NoOfTypes=$(($NoOfTypes -1))
    done
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    You'll need to change:

    Type=Inst_Type$NoOfTypes

    to:

    eval Type=\${Inst_Type$NoOfTypes}

    in order to get the value of the variable.

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Correct. My bad.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...