[英]How to fix a double/single quote inside the quotes in a MySQL 8 with BASH with EOF?
How to fix this error: 如何解决此错误:
# 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 '\''.
Or with double quotes: 或带双引号:
# 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 '\"'.
Without double/single quotes: 不带双/单引号:
# 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
Or with just double/single quotes: 或仅使用双引号/单引号:
# 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
The error is the same in a EOF shell file? EOF shell文件中的错误是否相同?
You don't need backslashes here. 您在这里不需要反斜杠。 You are confusing the command line for your heredoc and vice versa. 您混淆了heredoc的命令行,反之亦然。 You are also confusing mysql and the command line. 您还将混淆mysql和命令行。 Each of these (command line, heredoc, and mysql) have different rules regarding single and double quotes. 这些中的每一个(命令行,heredoc和mysql)对于单引号和双引号都有不同的规则。
Something like the following should work fine: 像下面这样的东西应该可以正常工作:
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
Your variables will be replaced by bash even though there are single quotes around them because the heredoc don't care. 即使变量周围有单引号,您的变量也会被bash替换,因为Heredoc不在乎。 This all then gets passed into mysql and mysql is happy because your string literals are properly quoted. 然后,所有这些都传递到mysql中,并且mysql很高兴,因为您的字符串文字已正确引用。
Lastly, if you are really wedded to those double quotes, you can use those instead inside your heredoc and it won't make a difference. 最后,如果您真的想过使用双引号,则可以在heredoc中使用这些双引号,这不会有任何不同。 bash will ignore them and mysql will allow them through. bash将忽略它们,而mysql将允许它们通过。
I lied, one other last thing. 我撒谎,另一件事。 You can use <<-
when declaring your heredoc so you can precede the lines in your heredoc with whitespace which makes it easier to read if you are doing this in a script. 声明heredoc时可以使用<<-
,因此可以在heredoc中的行之前加上空格,如果在脚本中执行此操作,则更易于阅读。 You can also name your heredoc whatever you want as long as it ends with the same word (both of these for script clarity/readability). 您也可以根据自己的喜好为Heredoc命名,只要它以相同的词结尾(这两个词都是为了使脚本清晰/易读)。 You also don't need cat
here as mysql can consume directly from a file, and a heredoc is in nearly every way that matters, a file. 您也不需要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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.