Find the answer to your Linux question:
Results 1 to 10 of 10
Running the following script: Code: #!/bin/bash a="one two three" b="a" echo $a echo $b Results in: one two three a Is it possible to return the value of $a by ...
  1. #1
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8

    bash: help with script

    Running the following script:

    Code:
    #!/bin/bash
    
    a="one two three"
    b="a"
    
    echo $a
    echo $b
    Results in:

    one two three
    a

    Is it possible to return the value of $a by calling $b? For example:

    echo ${$b} would result in "one two three" (it doesn't )

    I hope that is clear.

    Thanks,

    MG

  2. #2
    Just Joined! pmcoleman's Avatar
    Join Date
    Jan 2009
    Location
    Colorado Springs, CO USA
    Posts
    30
    Code:
    ~$ a="one two three"
    ~$ b=$a
    ~$ echo $b
    one two three
    Works here....

  3. #3
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    You're setting b to the variable $a. I have b set to the string "a". Consider the following script:

    Code:
    a="one two three"
    b="four five six"
    
    echo -n "Please select a or b"
    read choice
    Depending on the input, choice could be set to "a" or "b". Now if I wanted to reference the variable $a or $b based on that choice how would I do it?

    I can do this:

    Code:
    output="$""$choice"
    but if I do that then $output is seen as variable containing a string, not a variable containing a variable (eg, echo $output results in "$a" instead of "one two three")

    MG

  4. #4
    Just Joined! pmcoleman's Avatar
    Join Date
    Jan 2009
    Location
    Colorado Springs, CO USA
    Posts
    30
    Quote Originally Posted by mglenney View Post
    You're setting b to the variable $a. I have b set to the string "a". Consider the following script:

    Code:
    a="one two three"
    b="four five six"
    
    echo -n "Please select a or b"
    read choice
    Depending on the input, choice could be set to "a" or "b". Now if I wanted to reference the variable $a or $b based on that choice how would I do it?

    I can do this:

    Code:
    output="$""$choice"
    but if I do that then $output is seen as variable containing a string, not a variable containing a variable (eg, echo $output results in "$a" instead of "one two three")

    MG
    OK. I understand better...
    I went back through my own scripts looking for an example and found one that I have adapted to your original post. My approach to such scenarios is to use arrays in the script.

    Thus the following. Script is "self documenting" with output at the end.

    Code:
    ## Initiate variables
    a=0
    b=1
    ## Initiate array and populate elements 0 and 1
    arrayX[0]="one two three"
    arrayX[1]="four five six"
    ## Display current array elements
    echo "The current array element contents:"
    echo "Variable 'a'  ${arrayX[0]}" # Var from original example (variable a)
    echo "Variable 'b'  ${arrayX[1]}" # Var from original example (variable b)
    echo -n "Please select a or b :" # Per original post
    read choice
    echo "The value of choice is   $choice" # Display current value
    
    # convert for use with array
    if [ $choice = "a" ]
    	then choice=$a
    fi
    if [ $choice = "b" ]
    	then choice=$b
    fi
    echo "The value of choice is now   $choice" # Display converted value
    echo "Your selection is:  "
    # Display the "value" of the original post vars "a" or "b"
    echo ${arrayX[$choice]}
    exit 0
    
    ### OUTPUT ###
    # ~/Coding/WorkingScripts/Dev$ ./makeAchoice.bash 
    # The current array element contents:
    # Array 'a'  one two three
    # Array 'b'  four five six
    # Please select a or b :b
    # The value of choice is   b
    # The value of choice is now   1
    # Your selection is:  
    # four five six
    # ~/Coding/WorkingScripts/Dev$ ./makeAchoice.bash 
    # The current array element contents:
    # Array 'a'  one two three
    # Array 'b'  four five six
    # Please select a or b :a
    # The value of choice is   a
    # The value of choice is now   0
    # Your selection is:  
    # one two three
    # ~/Coding/WorkingScripts/Dev$
    As you can see the output changes based on the user input of selecting a or b.
    Instead of assigning values to a and b directly, the strings are held in the arrayX, elements 0 and 1. The user selection determines what element will be displayed.

    Good luck.. I have to get back to my other work, may not be able to check this forum until tomorrow.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The eval feature is present in many languages. It allows one to do indirect references (among other things). There is also a special syntax to do this in most recent versions of bash:
    Code:
    #!/bin/bash
    
    a="one two three"
    b="a"
    
    echo $a
    echo $b
    echo $$b
    eval echo $$b
    eval echo \$$b
    
    echo ${!b}
    producing:
    Code:
    % ./user3
    one two three
    a
    31635b
    31635b
    one two three
    one two three
    See Indirect References for more ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by mglenney View Post
    Running the following script:

    Code:
    #!/bin/bash
    
    a="one two three"
    b="a"
    
    echo $a
    echo $b
    Results in:

    one two three
    a

    Is it possible to return the value of $a by calling $b? For example:

    echo ${$b} would result in "one two three" (it doesn't )

    I hope that is clear.

    Thanks,

    MG
    why would you want to declare your "b" variable like that in the first place?
    Code:
    a="one two three"
    b="$a"

  7. #7
    Just Joined! pmcoleman's Avatar
    Join Date
    Jan 2009
    Location
    Colorado Springs, CO USA
    Posts
    30
    Quote Originally Posted by drl View Post
    Hi.

    The eval feature is present in many languages. It allows one to do indirect references (among other things). There is also a special syntax to do this in most recent versions of bash:
    Code:
    #!/bin/bash
    
    a="one two three"
    b="a"
    
    echo $a
    echo $b
    echo $$b
    eval echo $$b
    eval echo \$$b
    
    echo ${!b}
    producing:
    Code:
    % ./user3
    one two three
    a
    31635b
    31635b
    one two three
    one two three
    See Indirect References for more ... cheers, drl
    Very nice. I thought someone would jump in with a more effecient method. That is what I like about this forum, everyone can learn something new. ...adding your link to new things to check out. I was not aware of indirect references.

    Edit: Your link gave me back the name of the work I was looking for. I had lost the pdf version of the book some time back and my hard copy scripting volumes... well lets just say all is well now.. Thanks again.

  8. #8
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    Thanks drl!! This is exactly what I was looking for. Works like a charm.

    pmcoleman - Thanks for taking the time to try and work through this issue with me.

    MG

  9. #9
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by mglenney View Post
    Thanks drl!! This is exactly what I was looking for. Works like a charm.
    MG
    You're welcome. The tlpd is a very long document, too long for most people to take in all at once. However, it is one of the single best references around for bash scripting. I suggest you skim it every now and then if you are going to be doing scripting ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  10. #10
    Just Joined!
    Join Date
    Mar 2009
    Posts
    8
    ghostdog -

    The reason I had to do this is because where I work they have these bash scripts that are nothing but a bunch of variables containing the hostnames of our servers and they source these in other scripts so they can perform actions against a group of servers at one time.

    So they might have the following hosts.sh:

    Code:
    appHosts="host1 host2 host3"
    dbHosts="dbhost1 dbhost2 dbhost3"
    
    servers1="host1 dbhost1"
    servers2="host2 dbhost2"
    servers3="host3 dbhost3"
    
    allServers="$appHosts $dbhosts"
    And then the following getHostname.sh script:

    Code:
    #!/bin/bash
    
    . /nfs1/hosts.sh
    
    for name in $servers1
    do
      result=`ssh $name "hostname"`
      echo -ne "$name\t$result"
    done
    
    exit
    So I wanted to create a script to simplify running commands on remote hosts and was looking for a simpler way to call one of the host groups from the hosts.sh file without having to edit the script each time. What I did was add an additional variable to the end of hosts.sh called menuList:

    Code:
    menuList="appHosts dbHosts servers1 servers2 servers3 allServers"
    I use $menuList to generate a menu which the script user can use to select a group

    Code:
    function menu
    {
    i=1
    for name in `echo $menuList`
    do
      menu[$i]=$name
      echo -ne "$i)\t$name\n"
      let "i += 1"
    done
    
    echo ""
    echo -n "Please enter number: "
    read selection
    
    menuchoice=${menu[$selection]}
    }
    I just needed a way to tack the "$" back on to the front of the selected group to turn it into a variable.

    Hope that explains it. I wanted to post all of this in case you or someone else has a better way to accomplish it.

    Thanks,

    MG

Posting Permissions

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