# #思路1
Use awk to isolate the IP to 4 segments and then invoke it through a for loop
#!/bin/bashSTART_IP="192.168.2.1"j=1for i in `echo $START_IP|awk -F. ‘{print $1,$2,$3,$4}‘`do eval IP$j=$i echo "$IP"$j""
The main difficulty of this idea
1. The IP$j=$i
execution of the process will be error, only after the addition eval
of the normal execution
2. echo "$IP"$j""
, variable internal call variables, syntax issues
# #思路2
Use awk to isolate the IP from 4 segments, assign to the array variable, and then assign the data variable to four variables
#!/bin/bash#step1_advancedecho -e "\033[31mPlease input start IP\033[0m"read -e START_IPIP=(`echo $START_IP|sed ‘s/\./ /g‘`)IP1=`echo ${IP[0]}`IP2=`echo ${IP[1]}`IP3=`echo ${IP[2]}`IP4=`echo ${IP[3]}`echo -e "\033[31mPlease input node Numbers\033[0m"read -e NODE_NUMBERScat > hosts<<- END$IP1.$IP2.$IP3.$IP4 mds1$IP1.$IP2.$IP3.$[IP4+1] mds2ENDj=$[NODE_NUMBERS-1]for((i=1;i<j;i++))do echo "$IP1.$IP2.$IP3.$[IP4+1+i] oss$[i+1]" >> hostsdone
The main difficulty of this idea lies in
echo "$IP1.$IP2.$IP3.$[IP4+1+i] oss$[i+1]" >> hosts
Note on syntax formatting
# #思路3
Use awk to isolate 4 segments of IP, assign to array variables, and then directly invoke the variables of the array
#!/bin/bash#step1_advancedecho -e "\033[31mPlease input start IP\033[0m"read -e START_IPIP=(`echo $START_IP|sed ‘s/\./ /g‘`)echo -e "\033[31mPlease input node Numbers\033[0m"read -e NODE_NUMBERScat > hosts<<- END${IP[0]}.${IP[1]}.${IP[2]}.${IP[3]} mds1${IP[0]}.${IP[1]}.${IP[2]}.$[IP[3]+1] mds2ENDj=$[NODE_NUMBERS-1]for((i=1;i<j;i++))do echo "${IP[0]}.${IP[1]}.${IP[2]}.$[IP[3]+1+i] oss$[i+1]" >> hostsdone
The main difficulty of this idea is also
echo "${IP[0]}.${IP[1]}.${IP[2]}.$[IP[3]+1+i] oss$[i+1]" >> hosts
Note on syntax formatting
Shell about the. Separating IP into four segments and invoking several ideas