Enviornment Variable in B shell (I call it nested variable)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Enviornment Variable in B shell (I call it nested variable)
# 1  
Old 07-01-2008
Enviornment Variable in B shell (I call it nested variable)

Code:
#!/bin/sh

APP_ROOT_MODE1=/opt/app1.0
APP_ROOT_MODE2=/opt/app2.0

APP_ROOT=${APP_ROOT_${APP_MODE}}


# enviornment variable APP_MODE will be exported in the terminal where 
# we run the applciation,  its value is string - MODE1 or MODE2  
# My intension is:
# when export APP_MODE=MODE1 in terminal,  APP_ROOT is /opt/app1.0
# when export APP_MODE=MODE2 in terminal,  APP_ROOT is /opt/app2.0

But the script above does not work? Any grammar error? Any trick?

Thanks in advance
# 2  
Old 07-01-2008
Code:
Does not work

What does not work - what's the error output?

This here does not look as if it works:
Quote:
APP_ROOT=${APP_ROOT_${APP_MODE}}
Try
Code:
APP_ROOT=${APP_ROOT}_${APP_MODE}

# 3  
Old 07-01-2008
The erorr message is
line 24: ${APP_ROOT_${APP_MODE}}: bad substitution


BTW, ${APP_ROOT}_${APP_MODE} is not my intention.
# 4  
Old 07-02-2008
eval APP_ROOT=\${APP_ROOT_${APP_MODE}}
# 5  
Old 07-02-2008
Thanks, Annihilannic. Yes it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Programming

How to call a variable in awk again ?

Hi again and thanks to R.Singh. One more question here. The code works in awk. (or GAWK) awk 'BEGIN{print "Enter your Name: ";getline name < "-";print RS "Input entered by user is: "name}' How to display the variable name again ? The awk script is running automaticly to the... (3 Replies)
Discussion started by: Zabo
3 Replies

3. Shell Programming and Scripting

How to add second variable in awk nested if condition?

Hi Gurus, I have a command to assign value based on input value. current condition is "if pattern matches "case", then assign "HOLD" else "SUCC"right now, I need to add one more condition (variable name is VAR). the condition is "if pattern1 matches "case", then assign "HOLD" else if... (2 Replies)
Discussion started by: ken6503
2 Replies

4. Shell Programming and Scripting

Nested variable allocation

I've made a number of errors with this and am trying to work a solution within the same framework. /bin/ksh for host_name in ahost1 ahost2 bhost1 bhost2 do for host_prefix in a b do if echo ${host_name} | grep -qi ${host_prefix} then if ... (1 Reply)
Discussion started by: squrcles
1 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. UNIX for Advanced & Expert Users

How to parse nested variable

Hi, I want to parse nested variable in my script. for exp- c=1 G1='0318' G2='0023' G3='3092' G4='0014' while ;do g=G$c a=$g echo "Group=$g and value=$a" c=`expr $c + 1` done final output are as - --------------------------- Group=G1 and... (4 Replies)
Discussion started by: apskaushik
4 Replies

7. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

8. Shell Programming and Scripting

nested double quota and white space inside variable

I have a question about nested double quotes. Any help is appreciated. Here are my commands on Mac OS. # string="Ethernet \"USB Ethernet\" \"Bluetooth DUN\" AirPort FireWire \"Bluetooth PAN\"" # echo $string Ethernet "USB Ethernet" "Bluetooth DUN" AirPort FireWire "Bluetooth PAN" #... (3 Replies)
Discussion started by: lindazhou
3 Replies

9. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

10. Shell Programming and Scripting

Variable in While Loop Nested If

Hi all, I'm having problems with the setting a variable in a nested if statement. It doesn't seem to change even if it mets the 'if' condition. My script essentially looks for a user name from the output from a kerberos command. When I find the user name, I tried to change a variable and exit... (6 Replies)
Discussion started by: geass
6 Replies
Login or Register to Ask a Question