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 ...
- 03-17-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 8
bash: help with script
Running the following script:
Results in:Code:#!/bin/bash a="one two three" b="a" echo $a echo $b
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
- 03-17-2009 #2Works here....Code:
~$ a="one two three" ~$ b=$a ~$ echo $b one two three
- 03-18-2009 #3Just 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:
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?Code:a="one two three" b="four five six" echo -n "Please select a or b" read choice
I can do this:
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")Code:output="$""$choice"
MG
- 03-18-2009 #4
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.
As you can see the output changes based on the user input of selecting a or b.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$
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.
- 03-18-2009 #5Linux Engineer
- 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:
producing:Code:#!/bin/bash a="one two three" b="a" echo $a echo $b echo $$b eval echo $$b eval echo \$$b echo ${!b}
See Indirect References for more ... cheers, drlCode:% ./user3 one two three a 31635b 31635b one two three one two three
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 )
- 03-18-2009 #6Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 03-18-2009 #7
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.
- 03-18-2009 #8Just 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
- 03-18-2009 #9Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
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, drlWelcome - 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 )
- 03-18-2009 #10Just 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:
And then the following getHostname.sh script:Code:appHosts="host1 host2 host3" dbHosts="dbhost1 dbhost2 dbhost3" servers1="host1 dbhost1" servers2="host2 dbhost2" servers3="host3 dbhost3" allServers="$appHosts $dbhosts"
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:#!/bin/bash . /nfs1/hosts.sh for name in $servers1 do result=`ssh $name "hostname"` echo -ne "$name\t$result" done exit
I use $menuList to generate a menu which the script user can use to select a groupCode:menuList="appHosts dbHosts servers1 servers2 servers3 allServers"
I just needed a way to tack the "$" back on to the front of the selected group to turn it into a variable.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]} }
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


Reply With Quote
