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 18.104.22.168 22.214.171.124 "
$ 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 */ /");
echo result=$result node=$node nodes=$nodes ;
Notice : my sed replace
[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.
result=0 node=127.0.0.1 nodes=127.0.0.1 126.96.36.199 188.8.131.52
result=1 node=184.108.40.206 nodes=127.0.0.1 220.127.116.11
result=1 node=18.104.22.168 nodes=127.0.0.1