在bash中执行mysql命令时,不要回显特定的错误消息

时间:2021-02-03 16:24:13

I have to execute following mysql command via bash scripting:

我必须通过bash脚本执行以下mysql命令:

drop table $tablename \; | mysql -uusername -ppassword $dbname

This command is executed a lot of times, but the problem is that $tablename can be dropped before the command is run. And when it is dropped already, I get following message on my command prompt:

此命令执行了很多次,但问题是在运行命令之前可以删除$ tablename。当它已被删除时,我在命令提示符下收到以下消息:

ERROR 1051 (42S02) at line 1: Unknown table 'tablename'
database table tablename, not found

Is there a way to filter this error message out so I don't get spammed with this?

有没有办法过滤此错误消息,所以我不会被这个垃圾邮件?

EDIT: And is there a similar workaround like IF EXISTS for when creating tables? For preventing getting the following Table already exists.

编辑:在创建表格时是否有像IF EXISTS类似的解决方法?为防止获得以下表已存在。

Ok, it's IF NOT EXISTS thanks

好吧,如果不是,请回答谢谢

3 个解决方案

#1


2  

Add if exists:

如果存在则添加:

 drop table if exists $tablename

From the documentation:

从文档:

Use IF EXISTS to prevent an error from occurring for tables that do not exist. A NOTE is generated for each nonexistent table when using IF EXISTS.

使用IF EXISTS可以防止对不存在的表发生错误。使用IF EXISTS时,会为每个不存在的表生成注释。

#2


0  

If you look at the DROP TABLE syntax, you'll see that there's a IF EXISTS clause that's designed to deal with the fact that the table might not exist.

如果查看DROP TABLE语法,您将看到有一个IF EXISTS子句,旨在处理表可能不存在的事实。

As such, using...

因此,使用......

DROP TABLE IF EXISTS $tablename \; | mysql -uusername -ppassword $dbname

...will resolve this issue.

......将解决这个问题。

#3


-2  

I also discoverd that ... 2>/dev/null can be used. Which will write the output to /dev/null, and outputs nothing on the command prompt...

我也发现... 2> / dev / null可以使用。将输出写入/ dev / null,并在命令提示符下输出任何内容......

drop table $tablename \; | mysql -uusername -ppassword $dbname 2>/dev/null

/dev/null is called the black hole

/ dev / null被称为黑洞

#1


2  

Add if exists:

如果存在则添加:

 drop table if exists $tablename

From the documentation:

从文档:

Use IF EXISTS to prevent an error from occurring for tables that do not exist. A NOTE is generated for each nonexistent table when using IF EXISTS.

使用IF EXISTS可以防止对不存在的表发生错误。使用IF EXISTS时,会为每个不存在的表生成注释。

#2


0  

If you look at the DROP TABLE syntax, you'll see that there's a IF EXISTS clause that's designed to deal with the fact that the table might not exist.

如果查看DROP TABLE语法,您将看到有一个IF EXISTS子句,旨在处理表可能不存在的事实。

As such, using...

因此,使用......

DROP TABLE IF EXISTS $tablename \; | mysql -uusername -ppassword $dbname

...will resolve this issue.

......将解决这个问题。

#3


-2  

I also discoverd that ... 2>/dev/null can be used. Which will write the output to /dev/null, and outputs nothing on the command prompt...

我也发现... 2> / dev / null可以使用。将输出写入/ dev / null,并在命令提示符下输出任何内容......

drop table $tablename \; | mysql -uusername -ppassword $dbname 2>/dev/null

/dev/null is called the black hole

/ dev / null被称为黑洞