大佬教程收集整理的这篇文章主要介绍了在 ssh 命令中转义单引号,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 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
,
由于变量 $from
和 $to
是单引号字符串 ('
) 的一部分,bash 不会将它们扩展为值。
When to wrap quotes around a sHell variable?
请尝试以下操作:
#!/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;"
以上是大佬教程为你收集整理的在 ssh 命令中转义单引号全部内容,希望文章能够帮你解决在 ssh 命令中转义单引号所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。