【发布时间】:2021-05-14 20:21:41
【问题描述】:
似乎 sed 没有替换我代码中的字符串。 我打算让它做的是将名为“notification.enable”的属性从 server2 的 false 更改为 true 以激活,并将 server1 中的 true 更改为 false 以停用。 当我在下面单独使用它时它可以工作,但当从函数调用时它不起作用。
sed -i 's/notification.enable=false/notification.enable=true/g' /usr/config/activate.properties
function prompt_activate_server2_config {
activate="notification.enable=true"
deactivate="notification.enable=false"
dir=/usr/config
filename=$dir/activate.properties
output=$(grep "notification" $filename)
if [ -e $filename ];
then
sed -i 's/$deactivate/$activate/g' "$filename"
echo "Activated in Server 2"
echo $output;date
fi
}
function prompt_deactivate_server1_config {
activate="notification.enable=true"
deactivate="notification.enable=false"
dir=/usr/config
filename=$dir/activate.properties
output=$(grep "notification" $filename)
if [ -e $filename ];
then
sed -i 's/$activate/$deactivate/g' "$filename"
echo "Deactivated in Server 1"
echo $output;date
fi
}
我正在使用函数,因为我需要从另一台服务器远程执行脚本
所以我用下面的方法来称呼他们。所有“执行”的东西都在上述功能上。我错过了什么?
if [ "$selection" == 'Activate Config' ]; then
#Activate server1/deactivate server2#
ssh user@server2 "$(typeset -f prompt_activate_server2_config); prompt_activate_server2_config"
sleep 2
ssh user@server1 "$(typeset -f prompt_deactivate_server1_config); prompt_deactivate_server1_config"
fi
【问题讨论】:
-
请阅读minimal reproducible example。我们只需要查看失败的代码。祝你好运。
标签: unix