Bash For Loop with If& Elif Logic Broken

时间:2016-08-17 18:08:24

标签: bash

我已经玩过40天的bash脚本,有0经验,所以请原谅我,如果我的代码看起来像废话。我有一个脚本,将配置的NTP服务器从/etc/ntp.conf文件中取出(/root/ntp.conf进行测试)

NTPSRVCounter=1
echo "--- NTP Configuration ---"
echo " "
while read -r line; do
    if [ $NTPSRVCounter == 1 ] ; then
        echo "Primary NTP: $line"
        SEDConfiguredNTP1="$(echo $line | sed 's/\./\\./g')"
        ((NTPSRVCounter++))
        echo " "
            else
        SEDConfiguredNTP2="$(echo $line | sed 's/\./\\./g')"
        echo "Secondary NTP: $line"
        echo ""  
    fi
    done < <(grep -o -P '(?<=server ).*(?= iburst)' /root/ntp.conf)

并询问您是否要使用案例陈述更改它:

echo "Do you wish to change it? [Y/n]"
NTPSRVCounter2=1
read opt
case $opt in
    Y|y) read -p "Enter in your primary NTP server: " -e -i '0.debian.pool.ntp.org' UserInputNTP1
    read -p "Enter in your secondary NTP serer: " -e -i '1.debian.pool.ntp.org' UserInputNTP2
    for NTP in "$UserInputNTP1" "$UserInputNTP2" ; do
        is_fqdn "$NTP"
            if [[ $? == 0 && $NTPSRVCounter2 == 1 ]] ; then
                SEDUserInput1=$(echo $UserInputNTP1 | sed 's/\./\\./g')
                ((NTPSRVCounter2++))
            elif [[ $? == 0 && $NTPSRVCounter2 == 2 ]] ; then 
                SEDUserInput2=$(echo $UserInputNTP2 | sed 's/\./\\./g')
                sudo sed -i "s/$SEDConfiguredNTP1/$SEDUserInput1/g" /root/ntp.conf
                sudo sed -i "s/$SEDConfiguredNTP2/$SEDUserInput2/g" /root/ntp.conf
            else
                echo "Fail!!! :-( "
            fi
        done

     ;;

     N|n) return 0

     ;;

     *) echo "I don't know what happened, but, eh, you're not supposed to be here."
     ;;
     esac

问题出在&#34; elif&#34;声明和功能&#34; is_fqdn&#34;在第二次运行的功能。如果我把&#34; bash -x&#34;在脚本上运行它,我看到&#34; is_fqdn&#34;在函数的两次运行中返回0,但是elif语句&#34; $?&#34;是1而不是0。

使用的两个功能如下。必须将NTP地址验证为有效域名或I.P.地址,对吗? :)

is_fqdn() {
hostname=$1
if [[ "$hostname" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
    valid_ip "$hostname"
elif [[ "$hostname" == *"."* && "$hostname" != "localhost." && "$hostname" != "localhost" ]] ; then
    return 0
else
    return 1
fi
host $hostname > /dev/null 2>&1 || return 1
}


valid_ip(){
local stat=1
local ip=$1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
    OIFS=$IFS
    IFS="."
    ip=($ip)
    IFS=$OIFS
    [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
    stat=$?
fi
return "$stat"
}

1 个答案:

答案 0 :(得分:1)

if中的条件设置$?的值,这是elif部分中条件使用的值,而不是is_fqdn的返回值。如果要在多个位置使用它,则需要保存该值:

is_fqdn "$NTP"
is_fqdn_rv=$?
if [[ $is_fqdn_rv == 0 && $NTPSRVCounter2 == 1 ]] ; then
  SEDUserInput1=$(echo $UserInputNTP1 | sed 's/\./\\./g')
  ((NTPSRVCounter2++))
elif [[ $is_fqdn_rv == 0 && $NTPSRVCounter2 == 2 ]] ; then 
  SEDUserInput2=$(echo $UserInputNTP2 | sed 's/\./\\./g')
  sudo sed -i "s/$SEDConfiguredNTP1/$SEDUserInput1/g" /root/ntp.conf
  sudo sed -i "s/$SEDConfiguredNTP2/$SEDUserInput2/g" /root/ntp.conf
else
  echo "Fail!!! :-( "
fi