Hi

I am assigning some values like this in my script.

server1=admin
server2=managed
server3=managed1
count=3

while i in $count

With in while i like to get the admin, Managed and Managed1 values.

What i did is

val=server$i
echo $val (it prints server1 with in loop, but i like to get the value of the server1.ie admin)

i tried like echo $($val). I know this is stupid thing.

Can anyone guid me to access values for this assigned variable please?


Jasmine

Recommended Answers

All 11 Replies

use the following:

eval var=server$i

use the following:

eval var=server$i

Thank u for ur response.

I am not having problem in assigning the server1, server2, server3 in var. I like to get the value of server1(admin) if val' s value is server1 and value server2(Managed) if val's value is server2.

Please help me regarding this.

Jasmine

like this?

server1=bogus
val=server$i
eval echo \$$val

like this?

server1=bogus
val=server$i
eval echo \$$val

It is giving this output of "$server1".I think it is not evaluating.Please help me out.

Jasmine

Post the snippet of code using this, please. So that I can make a suggestion on a
method/fix that will work for you.

#!/usr/bin/ksh
. /app/bea/wl812/server/bin/setWLSEnv.sh >>/dev/null
server_count=3
server1=Mged
serevr2=Mged1
server3=myserver
port=7001
hostname=`hostname`

i=1
while (( i<= $server_count ));
do
eval val=server$i
val1=eval echo \$$val
X=`java weblogic.Admin -url t3://localhost:$port GETSTATE $val1`

Y=`echo $X | cut -d= -f6`
Z=`echo $Y | cut -d} -f1`
if [ "$Z" =  "RUNNING" ] ; then
        print rnnuing
else
echo "$serveri is not running" | mailx -s "" [email]jselvaraj@gmail.com[/email]
fi
(( i = i + 1))
done

I am getting the server status in this program. Number of servers and names are different on each server. So i have to write ingenaralized format.

Please help me. Thank you in advance.

Jasmine

Try replacing the following:

server_count=3 
server1=Mged 
serevr2=Mged1 
server3=myserver
.....
i=1 
while (( i<= $server_count )); 
do 
eval val=server$i 
val1=eval echo $$val 
.....
echo "$serveri is not running" | mailx -s "" [email]jselvaraj@gmail.com[/email] 
.....

with this:

set -A server Mged Mged1 myserver
.....
i=0
while (( i < ${#server[*]} ));
do
  val1=${server[$i]}
.....
  echo "$val1 is not running" | mailx -s "" jselvaraj@gmail.com
.....

the set -A creates a korn shell array. The first argument is the array name
with all additional arguments the values. The array index begins with 0.

This code can also easily be expanded by simply adding additional values to the
set -A line.

the construct ${#arrayname[*]} will return the number of items in the array,
which will be one greater than the last index (index starts with 0).

the construct ${arrayname[index]} will return the value stored at that index.

$arrayname or ${arrayname} or ${arrayname[*]} will return a space seperated
list of all items stored in the array (i.e. Mged Mged1 myserver) the will make the
use of the server_count and val variables needless. Hope this helps.

You can also assign to an array in the following manner:
arrayname[index]=value

You can also do this to increase an array by simply using an index one
greater that the last one which can be done without first checking the
current length with this convoluted construct:
arrayname[${#arrayname[*]}]=value

Really it helped me. I never thought about the array usage here. It is working good. Thank u very much.

Simple string comparison is kicking me off in the same program.

if [[ $Y = RUNNING ]]; then
echo "Running"
fi

Eventhough Y is having the value of RUNNING, it is not coming in to the loop.

I tried all the combination of if conditional statement by specifying that quote , with out quote, single bracket , double bracket, single equal and double equal . It never worked for me. Please help me.

FYI the same string comparison with the same syntax is working good in another program. Here the variable Y gets its vaule dynamicaly at the end of execution of some command from the program.

Jasmine

It looks okay to me. I can't see the rest of the program around it so I will go off on a limb
and say that maybe $Z should be variable here (at least if you have done it in the same way
as the earlier snippet of code). Also, as a debug option I can only say to try doing the
following directly before the if statement:

echo "===${Y}==="

the equals signs on both sides will let you see if the variable contains any thing other than
RUNNING. Maybe a couple of spaces or something, I don't know.

But in any case the construct is good.

Hi,

U r gem!!!! i dont have words to say thanks..

when i did echo "===${Y}===", i found that leading space in the string..

It is working now.

Thank u very much

Jasmine

No problem. glad to be of help. Good luck with any future endeavers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.