Variable Substitution Issue


 
Thread Tools Search this Thread
Operating Systems Solaris Variable Substitution Issue
# 1  
Old 02-15-2008
Variable Substitution Issue

#!/bin/ksh

VAR_ONE=HELLO

TEMP=ONE

echo $VAR_${TEMP}

## Output is: ONE

Hi, I want the output to echo HELLO and not ONE as the above script does. I know I am missing something with dollar substitution. Can anyone help me out ?
Thanks.
Cal
# 2  
Old 02-15-2008
eval echo \$VAR_$TEMP

This 3-word expression goes thru the last word twice- the first iteration by eval which evaluates the expression followin it- where it expands the var $TEMP to 'ONE', and also unescapes the first backslash.

This leaves 'echo $VAR_ONE'. echo does its job, and you get:
hello.

cheers, hope this helps.

EDIT: Didnt test this in ksh now, but I think this is how I did it at work...
# 3  
Old 02-15-2008
Quote:
Originally Posted by fimblo
eval echo \$VAR_$TEMP

This 3-word expression goes thru the last word twice- the first iteration by eval which evaluates the expression followin it- where it expands the var $TEMP to 'ONE', and also unescapes the first backslash.

This leaves 'echo $VAR_ONE'. echo does its job, and you get:
hello.

cheers, hope this helps.

EDIT: Didnt test this in ksh now, but I think this is how I did it at work...

Thanks this solution works but I want to assign the output to a variable.... its not working. I tried both

Q=eval echo \$VAR_$TEMP
Q=`eval echo \$VAR_$TEMP`

echo $Q doesnt give same output as eval echo \$VAR_$TEMP
# 4  
Old 02-15-2008
hmm it works for me, but then I never use backticks (unless Im in vanilla bourne shell). With backticks the output I get is 'ONE'. I use the dollar-parens syntax since you can nest it and its easier to read and debug.

Code:
$ VAR_ONE=hello
$ TEMP=ONE
$ eval echo \$VAR_$TEMP
hello
$ f=$(eval echo \$VAR_$TEMP)
$ echo $f
hello

# 5  
Old 02-16-2008
Quote:
Originally Posted by calredd
#!/bin/ksh

VAR_ONE=HELLO

TEMP=ONE

echo $VAR_${TEMP}

## Output is: ONE

Hi, I want the output to echo HELLO and not ONE as the above script does. I know I am missing something with dollar substitution. Can anyone help me out ?
Thanks.
Cal
Thanks, it works. Appreciate all the responces.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Substitution Issue with nawk

Hi, I'm trying to reformat some badly formatted XML that I've extracted from Oracle clob columns using the following nawk command: nawk '{gsub(/</,/>\n/); print}' test.raw > test.xml the substitution executes fine, but instead of subbing < with > followed by newline, it subs the < with a... (3 Replies)
Discussion started by: sffuji
3 Replies

2. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

3. Shell Programming and Scripting

Issue with substitution using sed

Hi all Having issue with substitution using sed Trying to assign the absolute path of the file to the variable 'floc' returned by the find command floc=`find / -name $fname` eg cat $floc '/root/samplecheck/myfile' I want to replace '/' with '->' in the 'floc' i am using the below sed... (2 Replies)
Discussion started by: amithsebkanattt
2 Replies

4. Shell Programming and Scripting

sed pattern substitution issue?

Hello everyone ... I'm going crazy, I hope some of you can help me ... I have to replace a line in a crontab like this: 5 2 * * 2 root backupdat with this: 5 5 * * 3 root backupdat the command I use is the following: sed -i.bak -e 's/5 2 * * 2 root backupdat/5 5 * * 3 root... (4 Replies)
Discussion started by: ionral
4 Replies

5. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

6. Shell Programming and Scripting

Issue in substitution

Hi , I have have file which has following structure 01aaaa88888000-9999 01ssss77777000-0991 01ssss7777700000991 02ssss7777700000991 The record 01 is corrupt as value from 12th field to 19th should be positive or start with - however it is 000-9999 it should be -0009999 i need to... (4 Replies)
Discussion started by: test_user
4 Replies

7. Shell Programming and Scripting

Variable Substitution

Hi , I have a variable as follows, Temp=`cat ABC.txt | cut -c5-` This will yeild a part of the date. say , 200912. I would like to substitute this variable's value in a filename. eg: File200912F.zip when i say File$TempF.zip , it is not substituting. Any help ? Thanks in... (2 Replies)
Discussion started by: mohanpadamata
2 Replies

8. Shell Programming and Scripting

encrytion/substitution issue in a file

1) ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547425069636596::6:N:mrs charles:N:PH:00010031:0001' OUTPUT - ABC::2197.12:2197.12:120217144365::+DEF:INT:1:N::::6:550.00:0.00:2009-04-29:CN:4547******636596::6:N:mrs charles:N:PH:00010031:0001' The... (5 Replies)
Discussion started by: mad_man12
5 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. Shell Programming and Scripting

Variable Substitution

I have run into a wall with my iptables firewall scripting. I am blocking all of the private side IP addresses on the WAN interface on systems running NAT. However, if the system is not running NAT and needs to allow access to the local LAN on the WAN interface, I need to block all but one of... (2 Replies)
Discussion started by: garak
2 Replies
Login or Register to Ask a Question