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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I create a 2 dimensional array

Status
Not open for further replies.

Arrumac

Technical User
Jan 30, 2002
8
0
0
GB
I want to write a script which will read the group file for about 200 unix servers into a 2 dimensional array and output that array to a comma sepparated file. The GID's should form the rows (I currently have up to GID 3000) and the server should form the columns. Is this possible?
 
Don't think you can in a shell script. ksh only supports 1-dimensional arrays. You could probably do something with awk, or write a simple C program to do it.

If I understand correctly, do you want your final file to look something like this?
Code:
server1, 1,2,3,4,10,........
server2, 2,4,5,7,9,......
server3,1,3,5,6,........
Greg.
 
Something like this may work. It depends on how you intend to get at the /etc/group file on each server. This one uses rsh, and assumes you have a list of servers in a file called serverlist, 1 per line.
Code:
#!/bin/ksh

for SERVER in $(cat serverlist)
do
  rsh $SERVER cat /etc/group |
  nawk -v server=$SERVER -F: 'BEGIN {printf("%s",server)}
  {printf(",%s",$3)}
  END {print}'
done
Greg.
 
As I understand it, you're trying to compile a list of what's on each server in your data center...

You should have a single file on your trusted host, that lists all of the servers you want to communicate with...

From your trusted host, you can issue a command similar to the following:

for SERVER in `cat $SERVER_LIST`
do
remsh $SERVER -l root -n cat /etc/group | awk -F: '{print $1, $3}'` >> /tmp/${SERVER}_Group_File
done

This will gather the required information from each server, and create a unique group file for each server, in the /tmp directory...

If you want one single file that contains all of the data from all of the servers, you need to add the server name to the data that you're including inside of your destination file...

This is a little more tricky...

for SERVER in `cat $SERVER_LIST`
do
GROUP = `remsh $SERVER -l root -n cat /etc/group | awk -F: '{print $1}'`
GID = `remsh $SERVER -l root -n cat /etc/group | awk -F: '{print $3}'`
printf "%-10s %-6s\n" "$SERVER","$GROUP","$GID" >> /tmp/$SERVER_Group_File
done

Hope this helps...

Joe F.
 
Oops...

The last printf statement is missing a 3rd format definition...

i.e.

printf "%-10s %-12s $-s\n" "$SERVER","...

Another quick way to do it, without formatting is:

echo $SERVER $GROUP $GID >> /tmp/$SERVER_Group_File

Joe F.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top