我有一个 for 循环来检查我在文件中的节点列表是否还活着:
#!/bin/bash
tmpnodes=$(cat $1)
#declare -a nodes
nodes=()
for node in $tmpnodes
do
result=$(ping -q -c 1 -W 2 $node >/dev/null 2>&1; echo $?)
echo "Result: $result, on node $node"
if [ $result -eq 0 ] ; then
nodes+=("$node")
#nodes=(${nodes[@]} "$node")
fi
done
echo "pingable nodes:"
for node in $nodes
do
echo "$node"
done
(我也试过这个版本declare
)
虽然我有更多可 ping 的节点,但我总是得到这个输出:
$ ./script.sh nodes
Result: 1, on node clab148node11
Result: 1, on node clab148node12
Result: 0, on node clab148node15
Result: 0, on node clab148node16
Result: 0, on node clab148node17
Result: 0, on node clab148node18
pingable nodes:
clab148node15