如何在带有 EOF 的 BASH 的 MySQL 8 中修复引号内的双引号/单引号?

  • 本文关键字:单引号 EOF MySQL BASH mysql shell eof
  • 更新时间 :
  • 英文 :


如何修复此错误:

# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to ''zabbix''@''localhost'' identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR at line 2: Unknown command '''.

或用双引号:

# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to """zabbix""@""localhost""" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR at line 2: Unknown command '"'.

不带双引号/单引号:

# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by ''' at line 1

或者只用双引号/单引号:

# cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
> create database zabbix character set utf8 collate utf8_bin; create user zabbix@localhost identified by '${ZABBIX_PASSWD}';
> grant all privileges on zabbix.* to "zabbix"@"localhost" identified by '${ZABBIX_PASSWD}';
> flush privileges;
> exit
> EOF
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by ''' at line 1

EOF 外壳文件中的错误相同吗?

这里不需要反斜杠。您将命令行与 heredoc 混淆了,反之亦然。您还混淆了mysql和命令行。它们中的每一个(命令行,heredoc和mysql(对单引号和双引号都有不同的规则。

  1. Mysql 需要用单引号括起来的字符串文字(但也可以使用双引号,但这不是标准的(。
  2. bash显然有关于单引号和双引号的规则,但它们在这里不适用,因为这是一个Heredoc
  3. 你的赫里多克不在乎。您的 heredoc 中的内容被视为文件。单引号,双引号,随便什么。很酷的是 bash 仍然会交换变量,所以它就像一个超级文件,但它只是一个 heredoc。

类似以下内容的内容应该可以正常工作:

cat <<EOF | mysql -uroot -p${MYSQL_PASSWD}
create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
flush privileges;
exit
EOF

你的变量将被 bash 替换,即使它们周围有单引号,因为 heredoc 不在乎。然后这一切都被传递到 mysql 中,mysql 很高兴,因为你的字符串文字被正确引用了。

最后,如果你真的执着于这些双引号,你可以在你的 heredoc 中改用它们,这不会有什么不同。 Bash将忽略它们,MySQL将允许它们通过。

我撒谎了,最后一件事。您可以在声明 heredoc 时使用<<-,这样您就可以在 heredoc 中的行前面加上空格,如果您在脚本中执行此操作,则更容易阅读。你也可以随心所欲地命名你的 heredoc,只要它以相同的单词结尾(两者都是为了脚本清晰/可读性(。你也不需要cat这里,因为 mysql 可以直接从文件中使用,而 heredoc 几乎在所有方面都是重要的,一个文件。

mysql -uroot -p${MYSQL_PASSWD} <<-MYSQLCOMMANDS
create database zabbix character set utf8 collate utf8_bin; create user 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '${ZABBIX_PASSWD}';
flush privileges;
exit
MYSQLCOMMANDS

最新更新