I want to try my hand at Arrays in AWK. I've written a script that will scp multiple files to multiple IP addresses and I'm curious about a specific part of the script.
This is my script
#! /bin/awk -f
{
iplist[$1];
filelist[$2];
}
END {
for (i in filelist)
for (q in iplist){
print "scp"filelist[i],i " root@"iplist[q],q;
}
}
The part that I don't understand is here
print "scp"filelist[i],i " root@"iplist[q],q;
Why do I need to have the ',i' and the ',q'?
What is the purpose of these? Without them the script runs but does not place the values of the variables in the output.
Example of file the arrays are coming from
test_ip
xx.x.xx.21 /cat/dog/bird/
xx.x.xx.22 /dog/cat/test/
xx.x.xx.23 /home/foo/bar/
With ,i & ,q
scp /cat/dog/bird/ root@ xx.x.xx.23
scp /cat/dog/bird/ root@ xx.x.xx.21
scp /cat/dog/bird/ root@ xx.x.xx.22
scp /home/foo/bar/ root@ xx.x.xx.23
scp /home/foo/bar/ root@ xx.x.xx.21
scp /home/foo/bar/ root@ xx.x.xx.22
scp /dog/cat/test/ root@ xx.x.xx.23
scp /dog/cat/test/ root@ xx.x.xx.21
scp /dog/cat/test/ root@ xx.x.xx.22
Without ,i & ,q
scp root@
scp root@
scp root@
scp root@
scp root@
scp root@
scp root@
scp root@
scp root@
awkconcatenates strings without any explicit operator. Theprintoperator gets 3 arguments because of the 2 commas. For practical purposes, theprintcould be written more clearly as:print "scp", i, "root@", q(the semicolon isn't necessary).