Bash ssh命令中的转义单引号

Bash ssh命令中的转义单引号,bash,shell,sed,Bash,Shell,Sed,我正在尝试使用ssh发送sed命令 这是我的剧本: #!/bin/bash IP_PUBLIC="192.168.0.1" from="DB_HOST='my_host'" to="DB_HOST='192.168.0.2'" ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" ' sed -i "s/'"$from'"/

我正在尝试使用ssh发送sed命令

这是我的剧本:

#!/bin/bash

IP_PUBLIC="192.168.0.1"
from="DB_HOST='my_host'"
to="DB_HOST='192.168.0.2'"
    
ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" '
    sed -i "s/'"$from'"/'"$to'"/g" /path/to/file;
'
这会引发以下错误:

test: line 9: unexpected EOF while looking for matching `''
test: line 10: syntax error: unexpected end of file
我也试过了

#!/bin/bash

IP_PUBLIC="192.168.0.1"

ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" '
    sed -i "s/DB_HOST=\'my_host\'/DB_HOST=\'192.168.0.2\'/g" /path/to/file;
'
我也犯了同样的错误

文件内容

#!/bin/bash
DB_HOST='my_host'
需要

#!/bin/bash
DB_HOST='192.168.0.2'

与其考虑如何双重转义,不如使用stdin,用bash
declare-p
传递实际变量,用
declare-f
传递函数

IP_PUBLIC="192.168.0.1"
from="DB_HOST='my_host'"
to="DB_HOST='192.168.0.2'"
work() {
    sed -i "s/$from/$to/g" /path/to/file;
}
    
ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" bash -s <<EOF
$(declare -p from to)  # hand-pick transfer variable values
$(declare -f work)     # hand-pick transfer code
# everything is properly quoted, as it would be on localhost
work                   # execute the function.
EOF
IP_PUBLIC=“192.168.0.1”
from=“DB\u HOST='my\u HOST'”
to=“DB_HOST='192.168.0.2'”
工作(){
sed-i“s/$from/$to/g”/path/to/file;
}

ssh-oStrictHostKeyChecking=no root@“$IP_PUBLIC”bash-s不要考虑如何双重转义,而是使用stdin并使用bash
declare-p
传递实际变量,使用
declare-f
传递函数

IP_PUBLIC="192.168.0.1"
from="DB_HOST='my_host'"
to="DB_HOST='192.168.0.2'"
work() {
    sed -i "s/$from/$to/g" /path/to/file;
}
    
ssh -oStrictHostKeyChecking=no root@"$IP_PUBLIC" bash -s <<EOF
$(declare -p from to)  # hand-pick transfer variable values
$(declare -f work)     # hand-pick transfer code
# everything is properly quoted, as it would be on localhost
work                   # execute the function.
EOF
IP_PUBLIC=“192.168.0.1”
from=“DB\u HOST='my\u HOST'”
to=“DB_HOST='192.168.0.2'”
工作(){
sed-i“s/$from/$to/g”/path/to/file;
}

ssh-oStrictHostKeyChecking=no root@“$IP_PUBLIC”bash-s,因为变量
$from
$to
是单个引号字符串(
)的一部分,bash不会将它们扩展为值

请尝试以下操作:

#/bin/bash
IP_PUBLIC=“192.168.0.1”
from=“DB\u HOST='my\u HOST'”
to=“DB_HOST='192.168.0.2'”
ssh-oStrictHostKeyChecking=no root@“$IP_PUBLIC”“sed-i\”s/${from}/${to}/g\”/path/to/file;”

由于变量
$from
$to
是单个引号字符串(
)的一部分,bash不会将它们展开为值

请尝试以下操作:

#/bin/bash
IP_PUBLIC=“192.168.0.1”
from=“DB\u HOST='my\u HOST'”
to=“DB_HOST='192.168.0.2'”
ssh-oStrictHostKeyChecking=no root@“$IP_PUBLIC”“sed-i\”s/${from}/${to}/g\”/path/to/file;”

谢谢你的回答,但我不能使用
bash-s
,因为我还有其他命令,比如
systemctl start snmpd
soooo just
work(){sed-i“s/$from/$to/g”/path/to/file;其他命令,如;systemctl start snmpd;}
。将它们放入函数中。或者只是把它们放在
EOF
之前-我不明白这对不使用
bash-s
有什么影响。谢谢你的回答,但我不能使用
bash-s
,因为我还有其他命令,比如
systemctl start snmpd
soooo just
work(){sed-i“s/$from/$to/g”/path/to/file;其他命令,如;systemctl start snmpd;}
。将它们放入函数中。或者只是把它们放在
EOF
之前-我不明白不使用
bash-s会有什么影响。