BASH - Reference external variable name dynamically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - Reference external variable name dynamically
# 1  
Old 11-15-2010
Tools BASH - Reference external variable name dynamically

Hi there,

I have included an external properties file into my BASH script via the 'source' command.

I am attempting to dynamically assign a variable in the BASH script, that references the variable name within the external properties file i.e.

#!/bin/bash
pth=${0%/*}
source $pth/common_refs.txt
source $pth/common_variables.txt

echo $env <----- variable from 'common_variables' which returns fine

returned value is 'prd_trs'

echo $prd_trs_ldaphost <---------- variable from 'common_refs' which returns fine

HOWEVER

echo $env"_ldaphost" <-------- this I expect to return the same value as $prd_trs_ldaphost from 'common_refs' file, but instead just echos:

'prd_trs_ldaphost'

Why is this happening and please can anyone suggest a work around?

Many thanks.
# 2  
Old 11-15-2010
Try:
Code:
eval echo \$$env"_ldaphost"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 11-15-2010
bash, other way
Code:
V=${env}_ldaphost
echo ${!V}

# 4  
Old 11-15-2010
MySQL

Code:
eval echo "\$${env}_ldaphost"
HOWEVER

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamically changing environment variable

Linux Redhat, BASH Shell. I want to put this in my .bash_profile I have log files that go to directory paths based in part on other variables such as went DB Name is set in memory. So if the DB Name changes the path to the log file changes. How do I create an environment variable I put into... (6 Replies)
Discussion started by: guessingo
6 Replies

2. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

3. Shell Programming and Scripting

Dynamically setting of environment variable... Can it be done?

Hi all, I am fairly new to unix scripting and will like to know how to dynamically set the name of an environment variable to be used. We have a .env file where we defined the names and locations of data files, trigger files, directories .... etc Example of variables defined in .env... (4 Replies)
Discussion started by: Morelia
4 Replies

4. Shell Programming and Scripting

Adding string variable dynamically in for loop

Hi, I need to generate the text name dynamically in for loop, ex, VAR_COPY_FILE1= file path 1 VAR_COPY_FILE2= file path 2 VAR_COPY_FILE3= file path 3 for i in 1 2 3 do if then "do some process here" fi done (3 Replies)
Discussion started by: msubash26
3 Replies

5. Shell Programming and Scripting

subsequently reference variable

Hello, This is not homework. It is a question that I received on a recent interview for a linux position. Can someone shed some light on the right answer? I got it wrong. Thanks, jaysunn (3 Replies)
Discussion started by: jaysunn
3 Replies

6. Shell Programming and Scripting

Unix Variable Reference and Substitution

I can't seem to make what appears to be a simple substitution. I want to define a list of systems for which daily reports need to be filed systems="systemA systemC systemZ" I then want to run a loop for i in ${systems} Analyze statistics Create the reports mailx (8 Replies)
Discussion started by: mugsymark
8 Replies

7. Shell Programming and Scripting

Reference Variable

Hi! I need to determin the most efficient way to do something (rather simple, I thought). I'm currently echo(ing) a series of menu options, and reading the command input as the number associated with the entry. What I need to do is when the option 1 is selected, that it references a list and... (18 Replies)
Discussion started by: cchaloux
18 Replies

8. Shell Programming and Scripting

How to reference a variable within sed?

Hi all, How can I use sed to perform a substitution if the string that I'm going to substitute is stored in a variable: Let's say: sed 's/abcdefg/good' VS tmp="abcdefg" sed 's/$tmp/good' The second case doesn't work. Guess it's due to the single quotes on the outside. How can I... (1 Reply)
Discussion started by: rockysfr
1 Replies

9. UNIX for Dummies Questions & Answers

How can i read array elements dynamically in bash?

Hi friends how can we read array elements dynamically in bash shell? (1 Reply)
Discussion started by: haisubbu
1 Replies

10. Shell Programming and Scripting

dynamically creating a variable name

Hi ! I have the following situation - ##First variable variableA=JOB_A ##bunch of other variable JOB_A_RESTART=cleanupJobA JOB_B_RESTART=cleanupJobB JOB_C_RESTART=cleanupJobC now i need a script which would - 1. take the first variable 2. create a new variable name... (2 Replies)
Discussion started by: hsahay
2 Replies
Login or Register to Ask a Question