需求:
指定一批ip列表,生成指定规模大小的redis cluster主从对应关系。
ip_list=(1.1.1.1 2.2.2.2 3.3.3.3 4.4.4.4 5.5.5.5) port=7001 len=${#ip_list[@]} cluster_size=20 tag=0 for ((i=0;i<=$len-1;i++)) do let tag=tag+1 if [[ $tag -le $cluster_size ]];then if [[ $i -eq $len-1 ]];then echo "${ip_list[$i]}:$port -> ${ip_list[0]}:$port" i=-1 let port=port+1 else echo "${ip_list[$i]}:$port -> ${ip_list[$i+1]}:$port" let port=port+1 fi else break fi done
测试结果
1.1.1.1:7001 -> 2.2.2.2:7001 2.2.2.2:7002 -> 3.3.3.3:7002 3.3.3.3:7003 -> 4.4.4.4:7003 4.4.4.4:7004 -> 5.5.5.5:7004 5.5.5.5:7005 -> 1.1.1.1:7005 1.1.1.1:7006 -> 2.2.2.2:7006 2.2.2.2:7007 -> 3.3.3.3:7007 3.3.3.3:7008 -> 4.4.4.4:7008 4.4.4.4:7009 -> 5.5.5.5:7009 5.5.5.5:7010 -> 1.1.1.1:7010 1.1.1.1:7011 -> 2.2.2.2:7011 2.2.2.2:7012 -> 3.3.3.3:7012 3.3.3.3:7013 -> 4.4.4.4:7013 4.4.4.4:7014 -> 5.5.5.5:7014 5.5.5.5:7015 -> 1.1.1.1:7015 1.1.1.1:7016 -> 2.2.2.2:7016 2.2.2.2:7017 -> 3.3.3.3:7017 3.3.3.3:7018 -> 4.4.4.4:7018 4.4.4.4:7019 -> 5.5.5.5:7019 5.5.5.5:7020 -> 1.1.1.1:7020