Bash将IP范围分解为变量

时间:2018-08-01 20:58:34

标签: regex bash ip pmta

我正在尝试编写一个bash脚本,它将执行以下操作

读取其中存储了IP地址的文件     ips.txt

获取这些IP地址并将其分为3个变量,这些变量将写入自己的文件中

Files
    thrid_octave.txt
    last_digit_third_octave.txt
    last_octive.txt

Variables 
    thrid_octave
    last_digit_third_octave
    last_octive

EX: 12.34.117.88

    thrid_octave = 111
    last_digit_third_octave = 7
    last_octive = 88

以下是我打算如何使用这些变量

    #!/bin/bash
    domain="$(cat domains.txt)"
    ip="$(cat ips.txt)"
    last_number_third_octete="$(cat last_number_third_octete.txt)"
    thrid_octive="$(cat thrid_octive.txt)"
    last_octive="$(cat last_octive.txt)"

    while read -r domain <&3 && read ip <&4 && read last_number_third_octete <&5 && read thrid_octive <&6 && read last_octive <&7 &&; 
    do 
    echo "processing $domain";


    "##################################### $domain ######################################################"
    smtp-listener $ip\n

    <virtual-mta $domain.c$last_number_third_octete.$last_octive>
    smtp-source-host $ip mailer1-vmta-plat-$thrid_octive-$last_octive.$domain
    </virtual-mta>

    <virtual-mta-pool $domain.p>
    virtual-mta $domain.c$thrid_octive-$last_octive
    </virtual-mta-pool>

    \n\n

    done 3<domains.txt 4<ips.txt 5<last_number_third_octete.txt 6<thrid_octive.txt 7<last_octive.txt

    echo "All Set"

1 个答案:

答案 0 :(得分:0)

您可以尝试使用cut,如下所示:

ip="12.34.117.88"
thrid_octave=`echo $ip | cut -d\. -f3`
last_digit_third_octave=`echo $ip | cut -d\. -f3 | cut -c 3`
last_octive=`echo $ip | cut -d\. -f4`