like @manatwork said, it looks simpler and safer to build a new list with the valid nodes.
However, as "$nodes" is evaluated before the first iteration of the for loop, it should be possible to change it during that same loop:
$ nodes="127.0.0.1 1.2.3.4 1.2.3.4 "
$ for node in $nodes; do
result=$(ping -c 1 $node >/dev/null 2>/dev/null; echo $?);
if [ "$result" -ne 0 ];
then nodes=$(echo $nodes | sed -e "s/ *$node */ /");
fi;
echo result=$result node=$node nodes=$nodes ;
done ;
Notice : my sed replace [space]$node[space]
or $node[space]
or [space]$node
to work in all cases depending on the position of $node
in the $nodes
list (If its in the middle, or the first or the last element, space(s) before and after it will be either present or not). And it only replace 1 occurence at a time, but you could add a final /g
to get rid of all occurences at once.
This outputs:
result=0 node=127.0.0.1 nodes=127.0.0.1 1.2.3.4 1.2.3.4
result=1 node=1.2.3.4 nodes=127.0.0.1 1.2.3.4
result=1 node=1.2.3.4 nodes=127.0.0.1
error=(); for node in $nodes; do ping -c 1 $node || error+=($node); done