ssh内的mysql客户端期望引用烦恼

时间:2012-07-09 12:57:03

标签: mysql bash ssh expect

我正在尝试在bash脚本中执行以下操作:

REPLICATION_USER_SETUP=$(expect -c "spawn ssh $LDAP_USER@$MASTER_IP_ADDRESS \"mysql --user=$MYSQL_USER --socket=$MYSQL_SOCKET --execute \"GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \'$MYSQL_REPLICATION_USER\'@\'$SLAVE_IP_ADDRESS\' IDENTIFIED BY \'$MYSQL_REPLICATION_PW\';\"\";expect Password:;send $LDAP_PASS\r;interact")

但是每当我尝试运行它时,我会在关闭引用后继续获得“额外字符”。 我尝试了双重和单个转义引号的变体,但似乎没有任何效果。 谁能告诉我这里我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试转义sql查询的反斜杠:

REPLICATION_USER_SETUP=$(expect -c "spawn ssh $LDAP_USER@$MASTER_IP_ADDRESS \"mysql --user=$MYSQL_USER --socket=$MYSQL_SOCKET --execute \\\"GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO \'$MYSQL_REPLICATION_USER\'@\'$SLAVE_IP_ADDRESS\' IDENTIFIED BY \'$MYSQL_REPLICATION_PW\';\\\"\";expect Password:;send $LDAP_PASS\r;interact")