ZBLOG

function version_lt () { test "$(sudo echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; } do_conf_check() { # base_release="24_08_3" new_release="$1" old_release=$(sudo ovs-appctl buildinfo |grep Release | awk -F'tianchi' '{pri...

经过优化的代码如下:

function version_lt () { test "$(sudo echo "$@" | tr " " "\n" | sort -rV | head -n 1)" != "$1"; }

do_conf_check() {
    # new_release为输入参数,old_release通过 ovs-appctl buildinfo 获取
    new_release="$1"
    old_release=$(sudo ovs-appctl buildinfo | grep Release | awk -F 'tianchi' '{print $2}')
    
    # 检查版本是否满足升级条件:old_release < 24_08_3,new_release >= 24_08_3
    if version_lt "$old_release" "24_08_3" && (version_lt "24_08_3" "$new_release" || [ "24_08_3" == "$new_release" ]); then
        declare -A conf_map=(
            ["/etc/cns.conf"]="/apps/conf/tianchi-openvswitch/cns.conf"
            ["/etc/pmd.conf"]="/apps/conf/tianchi-openvswitch/pmd.conf"
        )
        
        for key in "${!conf_map[@]}"; do
            value="${conf_map[$key]}"
            
            if [ -f "$value" ]; then
                sudo cp "$value" "$key"
                echo "$value has been copied to $key"
            else
                echo "$value does not exist"
            fi
        done
        
    # 检查版本是否满足降级条件:new_release < 24_08_3,old_release >= 24_08_3
    elif version_lt "$new_release" "24_08_3" && (version_lt "24_08_3" "$old_release" || [ "24_08_3" == "$old_release" ]); then
        declare -A conf_map=(
            ["/apps/conf/tianchi-openvswitch/cns.conf"]="/etc/cns.conf"
            ["/apps/conf/tianchi-openvswitch/pmd.conf"]="/etc/pmd.conf"
        )
        
        for key in "${!conf_map[@]}"; do
            value="${conf_map[$key]}"
            
            if [ -f "$value" ]; then
                sudo cp "$value" "$key"
                echo "$value has been copied to $key"
            else
                echo "$value does not exist"
            fi
        done
        
    fi
}

这是对原有代码进行了一些修正和调整,使其更加符合bash语法规范,并且增加了对配置文件是否存在的检查。请注意,以上代码仅供参考,请在实际使用前自行测试和验证。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=4882

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?