I have a function that gives me a list of IPs and for each IP in my list, I want to run a query. The problem I'm having is its only looping through (1) of the results and not the rest.
getPartition ()
{
_knife=$(which knife);
_grep=$(which grep);
_awk=$(which awk);
cd ~/home/foo/.chef
local result=$(${_knife} search "chef_environment:dev AND role:myapp AND ec2_region:us-east-1" | ${_grep} IP | ${_awk} '{ print $2 }');
read -a servers <<< $result;
echo "Checking ${#servers[@]} servers";
for i in ${servers[@]};
do
local host='10.1.2.123'
local db='mystate'
_mongo=$(which mongo);
echo -n "$i";
local exp="db.foobarcluster_servers.find(
{\"node_host\":\"${i}\",\"node_type\":\"PROCESS\",\"region\":\"us-east-1\",\"status\":\"ACTIVE\"},{\"partition_range_start\":1,\"partition_range_end\":1, _id:0}).pretty();";
${_mongo} ${host}/${db} --eval "$exp" | grep -o -e "{[^}]*}";
done
}
So, I tried using for, but its only running the query for (1) of the (5) hosts listed.
I can see in my output for result that the list of IPs look like this:
+ local 'result=10.8.3.34
10.8.2.161
10.8.3.514
10.8.4.130
10.8.2.173'
So, I'm just returning results for (1) of the IPs it should be (5) of them because I have 5 IPs:
+ read -a servers
+ echo 'Checking 1 servers'
Checking 1 servers
+ for i in ${servers[@]}
+ local host=10.1.2.130
+ local db=mystate
++ which mongo
+ _mongo=/usr/local/bin/mongo
+ echo -n 10.8.3.34
10.8.3.34+ local 'exp=db.foobarcluster_servers.find(
{"node_host":"10.8.3.34","node_type":"PROCESS","region":"us-east-1","status":"ACTIVE"},{"partition_range_start":1,"partition_range_end":1, _id:0}).pretty();'
+ /usr/local/bin/mongo 10.8.3.34/mystate --eval 'db.foobarcluster_servers.find(
{"node_host":"10.8.3.34","node_type":"PROCESS","region":"us-east-1","status":"ACTIVE"},{"partition_range_start":1,"partition_range_end":1, _id:0}).pretty();'
+ grep -o -e '{[^}]*}'
{ "partition_range_start" : 31, "partition_range_end" : 31 }
+ set +x
Results:
{ "partition_range_start" : 31, "partition_range_end" : 31 }
I'm expecting:
{ "partition_range_start" : 31, "partition_range_end" : 31 }
{ "partition_range_start" : 32, "partition_range_end" : 32 }
{ "partition_range_start" : 33, "partition_range_end" : 33 }
{ "partition_range_start" : 34, "partition_range_end" : 34 }
{ "partition_range_start" : 35, "partition_range_end" : 35 }
How do I effectively loop through my IPs? Did I set up result properly as a variable to hold that list of IPs?
${servers[@])?