Unix Variable Reference and Substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Variable Reference and Substitution
# 1  
Old 07-01-2004
Network 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
# 2  
Old 07-01-2004
The code you posted seems valid. Where's the problem? Did you forget the "do" on the line following "for"?
# 3  
Old 07-02-2004
Sorry - first time on the board and I did not finish my problem before I accidentally hit a key and posted it. Thanks for reply

Here is what I can not do. For each of those systems, I neeed to email the reports using mailx to different recipients.

systemA_list="me you"
systemB_list="me you him"
systemZ_list="you him"

Therefore, in the for do loop, I need to use the current value of the systems variable that is being processed to reference the correct ${systems}_list and poulate the mailx command.

It is that variable substitution or reference that I can not seem to acheive. it always tells me it can not find systemA_list. Not sure if it even knows I am trying to make it a variable.

Thanks
Mark
# 4  
Old 07-02-2004
With bash, you can use the following construct to use the contents of a variable as the name of another variable, doing in effect a double expansion:

${!systems}

If systems contains "systemA_list", then it will instead expand the value of $systemA_list. It sounds like you have to do something like this:

system=systemA

listname=${system}_list #put name of required variable in $listname

list=${!listname} # expand $listname twice, giving the value of $systemA_list
# 5  
Old 07-02-2004
How about ksh?

Not sayng I can't change shells but everything so far was done in ksh.

Appreciate your suggestions.

Mark
# 6  
Old 07-02-2004
I checked the ksh manual and couldn't find anything about indirect substitution, so I guess you're out of luck. I'm no ksh regular, though.
# 7  
Old 07-02-2004
Thanks for your time - I will be trying your suggestions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

3. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: mjwoodford
3 Replies

4. 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

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

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

7. Shell Programming and Scripting

Array reference - bad substitution

I've created a series of arrays named as follows: row1 row2 row3 . . . row10 Each has 4 elements. I'm trying to echo the array elements out in a for loop. Here's what I have: for ((i=1;i<=10;i++)) do for ((j=1;j<=4;j++)) do eval out=${row`echo $i`} echo -n $out (3 Replies)
Discussion started by: swankgd
3 Replies

8. 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

9. 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

10. 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
Login or Register to Ask a Question