经过优化的代码如下:
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助手提供,问题来源于学员提问