Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Calling a Variable Variable

Help
scotttyso
2010-07-07
2013-05-02
  • scotttyso
    scotttyso
    2010-07-07

    I am trying to figure out how to call a variable variable and I am unable to get it to work.  Let me first explain a little.  I have a script that I want to run the same process on multiple spawn instances without having to define the same process over and over.

    So my script is set to spawn a shell prompt and login to several devices.  I then create a variable for each spawn_id and the prompt of the device that I login to.

    I would like to do something like the following

    set devices "L1 L2 L3"
    foreach i $devices {
         spawn $env(SHELL)
         set $i\SHELL $spawn_Id
         expect -re "\((\>|%|#|\\\$) $\)" { set prompt$i $expect_out(1,string) }
    }

    Then I need to call these again later on so
    foreach i $devices {
    set spawn_id $$i\Shell

    The last line is what I just can't find any examples of how to do.  The variables are set fine up above but I can't call the variable that contains the variable "$$i\Shell" for example.
    I am not sure the syntax
    I have tried:
    $($i\Shell) **which gives me can't read "(L1Shell)": no such variable
    $`$i\Shell` **Gives me "can not find channel named $`L1Shell`"
    $ ** invalid command name "L1Shell"
    ${$i\Shell}  ** can't read "$i\Shell": no such variable

    Somehow I want to be able to call $L1Shell $L2Shell $L3Shell by by doing
    set device "L1 L2 L3"
    foreach i $device {
    set spawn_id $$iShell ! This syntax for this double variable I cannot figure out.  I lack the experience.
    }

     
  • ender
    ender
    2010-07-07

    Try this

    set spawn_id

     
  • scotttyso
    scotttyso
    2010-07-07

    You know you make it look so easy you make me feel silly.  Thank you so much.  That was spot on.  I have never had any formal programming experience but have been working with expect for about three years.  It is an amazing utility (even though I was getting pretty frustrated last night.)

    Thanks again.

     
  • ender
    ender
    2010-07-07

    I'm glad it worked. I can't take credit for making it look easy though, I just checked in the book for the answer.

     
  • scotttyso
    scotttyso
    2010-07-07

    I am assuming you are talking about exploring expect.  What page did you find it on.  Because I was looking all over the book last night.

     
  • ender
    ender
    2010-07-07

    page 51, indirect references

     
  • scotttyso
    scotttyso
    2010-07-07

    Thanks