shell脚本中以字符串作为变量名获取变量值的方式
worker_ip1='192.71.0.171'
i=1
##打印 ip的值为‘192.71.0.171’ 的值##
ip=$(eval echo \$worker_ip$i)
实际应用例(循环检测mpp worker节点并进行有效无效操作):
#!/bin/bash
master_ip='192.71.0.170'
master_port='5432'
worker_ip1='192.71.0.171'
worker_port1='5432'
worker_ip2='192.71.0.172'
worker_port2='5432'
worker_ip3='192.71.0.173'
worker_port3='5432'
worker_ip4='192.71.0.174'
worker_port4='5432'
worker_ip5='192.71.0.175'
worker_port5='5432'
worker_ip6='192.71.0.176'
worker_port6='5432'
while true
do
for i in {1..6}
do
ip=$(eval echo \$worker_ip$i)
port=$(eval echo \$worker_port$i)
# echo $ip
# echo $port
ping -c 5 $ip > /dev/null
if [[ $? = 0 ]]; then
echo 'ok!' > /dev/null
else
echo 'error!'> /dev/null
uxsql -h $master_ip -p $master_port -c "select master_disable_node('${ip}', ${port})" > dev/null
fi
done
continue
done