Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable substitution:Urgent

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
In my script, i have

#########
REPLICA_SERVER_1="sandbox1"
REPLICA_CNT=1

REPLICA_SERVER=REPLICA_SERVER_${REPLICA_CNT}
echo "replica:$REPLICA_SERVER"
###########

o/p should be: replica:sandbox1

But, i am getting o/p as replica: REPLICA_SERVER_1

Is there a way to get the required output.

Any help is appreciated.

Thanks
 
echo "replica:${REPLICA_SERVER_1}"

but what's the point? vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks! for the reply. I cannot directly use echo &quot;replica:${REPLICA_SERVER_1}&quot;
since the script doesnt know if it would be REPLICA_SERVER_1, REPLICA_SERVER_2
,REPLICA_SERVER_3 .....All the script which i provided will be included in a while
loop where REPLICA_SERVER is constructed dynamically. And its value is the
variablename present in config file.

Ex : In config file i have,

REPLICA_SERVER_1=&quot;xyz&quot;
REPLICA_SERVER_2=&quot;123&quot;

So, i have to read all the values from the conf file. Is there any other way.

Thanks! A lot.
 
Thanks! for the reply. I cannot directly use echo &quot;replica:${REPLICA_SERVER_1}&quot;
since the script doesnt know if it would be REPLICA_SERVER_1, REPLICA_SERVER_2
,REPLICA_SERVER_3 .....All the script which i provided will be included in a while
loop where REPLICA_SERVER is constructed dynamically. And its value is the
variablename present in config file.

Ex : In config file i have,

REPLICA_SERVER_1=&quot;xyz&quot;
REPLICA_SERVER_2=&quot;123&quot;

So, i have to read all the values from the conf file. Is there any other way.

Thanks! A lot.
 
one way would be [a bit clunky, but....]

echo &quot;replica:$(eval echo \$${REPLICA_SERVER})&quot; vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Hi Vgersh99,
Thanks! for the reply. When i use :
echo &quot;replica:$(eval echo \$${REPLICA_SERVER})&quot;

i get output as : replica:$(eval echo $ABOVENET_REPLICA_1)

O/p required : replica:xyz

I think the script needs little modification. Any ideas?

Thanks
 
make sure you're in ksh.
my test script is:
#!/bin/ksh

REPLICA_SERVER_1=&quot;sandbox1&quot;
REPLICA_CNT=1

REPLICA_SERVER=&quot;REPLICA_SERVER_${REPLICA_CNT}&quot;

echo &quot;replica:$(eval echo \$${REPLICA_SERVER})&quot;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
To set the REPLICA_SERVER variable, try:

Code:
eval REPLICA_SERVER=\$REPLICA_SERVER_$REPLICA_CNT

It is faster because no subshell is launched and it works for sh, ksh and bash.

The whole script is then:
Code:
REPLICA_SERVER_1=&quot;sandbox1&quot;
REPLICA_CNT=1

eval REPLICA_SERVER=\${REPLICA_SERVER_${REPLICA_CNT}}
echo &quot;replica:$REPLICA_SERVER&quot;
 
Hey !
In my previous post, my browser wrap the longest line:
the
Code:
eval
and the
Code:
REPLICA_SERVER=...
are on the same line !

Well, maybe the eval command will deserve some explanations.
The eval command is a shell builtin. Its arguments are parsed and evaluated (file pattern substitution, variables expantion, ...) like arguments of any other command then the result of the evaluation is taken as a new command line to parse and evaluate.

Add
Code:
set -xv
before the eval to displays the parsed command (before argument evaluation) and the executed command (after argument evaluation) preceded by a '+' sign. I get:
Code:
eval REPLICA_SERVER=\${REPLICA_SERVER_${REPLICA_CNT}}
+ eval REPLICA_SERVER=${REPLICA_SERVER_1}
+ REPLICA_SERVER=sandbox1
echo $REPLICA_SERVER
+ echo sandbox1
sandbox1

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top