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!

"Automatic" Array 2

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
DE
Hi folks,

I need something like an "automatic array" for some of my scripts ...

Here's an example:

I'd like to read a server list containing let's say 3 entries ...

wts1
wts2
dbb1

These entries shall be automatically put into an array. Like this:

srv[0]=wts1
srv[1]=wts2
src[2]=dbb1

... but that's not all !
It could happen that there are servers being added or removed from the server list and the array is supposed to automatically react to that ...

Is it possible ?
If so, how ?

Regards
Thomas
 
Use the for command to loop thru your list

for server in 'cat serverlist'
do
echo $server
done
 
To create the array
Code:
set -A arrayname val1 val2 val3
To append to the array
Code:
set -A arrayname ${arrayname[*]} new_val
To delete fifth member from the array
Code:
arrayname[4]="" # note offset for zero indexed array
set -A arrayname ${arrayname[*]}
I'm not sure what you mean by 'automatically' but that should get you started.

On the internet no one knows you're a dog

Columb Healy
 
Hmm...

I've reread your post. Assuming that the 'server file looks something like
Code:
wts1
wts2
wts3
dbs1
dbs2
dbs3
dbs4
then somthing along the lines of
Code:
set -A serverlist $(cat /path/to/serverfile)
will create an array. As an example, run under ksh on RedHat Linux
Code:
# cat /tmp/servers
wts1
wts2
wts3
wts4
dbs1
dbs2
dbs3
# set -A svr_list $(cat /tmp/servers)
# echo ${svr_list[*]}
wts1 wts2 wts3 wts4 dbs1 dbs2 dbs3
#

On the internet no one knows you're a dog

Columb Healy
 
Yet the UUOC patrol:
set -A srv $(</path/to/list)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
It works !
Thanks a lot !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top