本文共 682 字,大约阅读时间需要 2 分钟。
我有以下ssh脚本:
#Here I am getting the mysql password for the root user
password=$(cat /root/.my.cnf | grep "password" | awk -F\" '{print $2}')
#here I am trying to dump the database remotely
mysqldump -uroot -p$password $db | ssh root@$destination_server "cat > /backup/mysql/$db.sql"
问题是我遇到以下错误:
/bin/bash: -c: line 0: syntax error near unexpected token `)'
/bin/bash: -c: line 0: `mysqldump -uroot -pbyt)uy6 database_name '
我已经测试了很多,发现错误是由于密码中的特殊字符引起的。由于我是从.my.cnf文件中获取密码的,因此无法通过对转义字符进行硬编码来对其进行转义。
谁能为我提供一种方法,也许可以在$ password变量中搜索那些特殊字符并添加到这些转义斜杠(“ \”)中。
编辑:我忘了提到我是通过使用vzctl exec命令通过OpenVZ部分虚拟化执行此操作的,这就是为什么我需要一种允许我按要求转义特殊字符的方法的原因。我开发的方法是sed命令的简单使用:
sed 's/[!-+]/\\&/g'
谢谢大家的帮助。
最好的祝福!
转载地址:https://blog.csdn.net/weixin_33501587/article/details/114499664 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!