Shell about the. Separating IP into four segments and invoking several ideas

Source: Internet
Author: User

# #思路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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.