在 bash 脚本中运行 mysql 命令会产生错误 [英] Running mysql commands in bash script generating errors

查看:39
本文介绍了在 bash 脚本中运行 mysql 命令会产生错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行一个执行 MySQL 命令的 bash 脚本,我在这一行收到一个错误.

$MYSQL_BIN $DATABASE -e \"ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`";

错误是created_date: command not found

以及在这一行:

$MYSQL_BIN $DATABASE -e \"UPDATE `nodes` SET `created_date` = UNIX_TIMESTAMP() WHERE `created_date`IS NULL AND `address` 不是 NULL";

<块引用>

第 1 行的 ERROR 1064 (42000):您有SQL 语法错误;检查与您的 MySQL 相对应的手册正确语法的服务器版本在 'SET = UNIX_TIMESTAMP() 附近使用WHERE IS NULL AND IS NOT NULL' 在第 1 行.

我认为第一个错误是因为我可能没有转义引号?

解决方案

Bash 使用 bactick 操作符 (`) 表示命令替换,即将一个命令的标准输出替换为一行定义另一个命令的文本.

所以你应该使用单引号而不是双引号或者正确地转义反引号:

$MYSQL_BIN $DATABASE -e'ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`';

$MYSQL_BIN $DATABASE -e"ALTER TABLE \`nodes\` ADD COLUMN \`created_date\` int(32) AFTER \`address\`";

Running a bash script executing MySQL commands, I get an error on this line.

$MYSQL_BIN $DATABASE -e \
"ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`";

The error is created_date: command not found

As well as on this line:

$MYSQL_BIN $DATABASE -e \
"UPDATE `nodes` SET `created_date` = UNIX_TIMESTAMP() WHERE `created_date` 
IS NULL AND `address` IS NOT NULL";

ERROR 1064 (42000) at line 1: 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 'SET = UNIX_TIMESTAMP() WHERE IS NULL AND IS NOT NULL' at line 1.

I believe the first error is because I'm probably not escaping quotes?

解决方案

Bash uses bactick operator (`) to indicate command substitution, that is, substitution of the standard output from one command into a line of text defining another command.

So you should either use single quotes instead of double ones or escape the backticks properly:

$MYSQL_BIN $DATABASE -e 
'ALTER TABLE `nodes` ADD COLUMN `created_date` int(32) AFTER `address`';

or

$MYSQL_BIN $DATABASE -e 
"ALTER TABLE \`nodes\` ADD COLUMN \`created_date\` int(32) AFTER \`address\`";

这篇关于在 bash 脚本中运行 mysql 命令会产生错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆