0

我正在尝试编写一个 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"
4

1 回答 1

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`
于 2018-08-02T08:02:37.070 回答