parse a file and fill an array with it

Hi Guys,

I am trying to do a file parse which is something like

config file:
machines= sha1 sha2 sha3 sha4

The bash script should be supporting upto 64 such machines
what I want is to place the machines in an array and then use the array to ssh to each machine.

The script I worte

declare -x machine
x=$(gawk -F ' ' '/^machine/ {print NF}' a)
i=1

while [ $i -lt $x ]; do
machine=$(gawk 'BEGIN {FS=" "} ; /^machine/ && NF <= 64 {print $i}' a)
#do the SSH here
let "i+=1"
echo mach $machine
done

The script always prints the entire line instead of indivudual word.
Also, it only works if there a single entry of 'machine'

Can some please help in this.

Cheers,
nani

You can try something like this:

awk '{for(i=2;i<=NF;i++){print $NF}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards

Hi Franklin,
I tried to run by changing the config file to my config as mentioned erlier.
It prints onyl the last field (word) in the config file 3 times and does not fill anything else apart form it.

I did not understand why r u looping starting form '2'.
However, I changed it to 0 and 1 and did not get the expected result.

Regards,
Mukund Jampala

Looks like he had a typo in his code; try changing print $NF to print $i.

perhaps like this

awk '{print $1}' /tmp/config.txt|while read output;
do
machine=$(echo $output | awk '{ print $1}')
#your script here
echo $machine
done

config.txt is the machine list file
hope that help

Like Annihilannic mentioned, I had a typo in the code, and the loop begins with the second field after the "=" so I assume this is the first machine name. Maybe it should begins with the 3th field, try it out.

awk '{for(i=2;i<=NF;i++){print $i}}' "config.file" |
while read mach; do
  #do your stuff here
  echo "$mach"
done

Regards

Hi Franklin,

Thanks you, I got it working.
But, I want these details in a array with extra spaces removed.

However, I tried by adding a ssh command to it and does not work
When I do this the loop gets restricted to a single iteration. ?I did not see the second iteration happening. and it happens only the case of ssh.

Instead local commands works fine.

awk '{for(i=2;i<=NF;i++){print $i}}' "a" |
while read mach; do
#do your stuff here
echo "hello $mach"
ssh root@rna4 uname -n # <----
echo $?
done

Regards,
Mukund Jampala

Does the echo command give the proper names of the machines and does the ssh command work manually?

Hi Franklin,

It parse the file correctly.
I get the data correctly from the file
ssh indeed is passwd less ssh to all the nodes.

my a file
machine= sna4 sna3

But, when I try to ssh it only prints out the following (logged in as root)
#./tmp.sh
hello sna4
sna4
0

Where as it should also have

hello sna3
sna3
0

This is missing. Why is not clear.

Regards,
Mukund

Do you have spaces around the "="?
Post your line in the exact format within code tags.

Regards

Hi Franklin,

I don't know how to do that exactly.
machine has no space before it.
The first space is only after '='

Followed by, there is a single space between sna4 and sna3.
This is a direct copy from my txt file.

machine= sna4 sna3

Regards,
Mukund Jampala

Use ssh with the -n option, otherwise it gobbles up the stdin from the while/read loop which is why it only runs once.