醒目退出code执行MySQL查询后 [英] Catching exit code after executing mysql query

查看:148
本文介绍了醒目退出code执行MySQL查询后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个bash脚本,将查询的MySQL。我想补充一些错误检查。我们说,如果由于某种原因,下面的查询失败,我要赶在退出错误并退出脚本。例如,这是我的脚本的一部分。

I'm working on a bash script that will query mysql. I would like to add some error checking. Let say if the following query for some reason fails I want to catch the exit error and exit the script. For example this is part of my script.

QUERY="SELECT DISTINCT `TABLE_SCHEMA`, `TABLE_NAME`
FROM `information_schema`.`TABLES`
WHERE  table_schema NOT IN ( 'mysql', 'information_schema', 'performance_schema' )"    

mysql -u user -pPASSWD --batch -N -e "$QUERY" | while read DATABASE TABLE;
do
  ...
  ...
  ...
done

我怎么能抓住出口code后脚本运行$查询。我在想这样的事情。但它似乎并没有工作。

How could i catch the exit code after the scripts run the "$QUERY". I was thinking something like this. But it doesn't seem to work.

mysql -u user -pPASSWD --batch -N -e "$QUERY" echo $? | while read DATABASE TABLE;

任何想法

推荐答案

您是在好方法:<?code> $ 是检查标志:

You are in the good way: $? is the flag to check:

$ mysql -h mydb <<< "SELECT * FROM MyDB.some_table_that_exists;"
$ echo $?
0

$ mysql -h mydb <<< "SELECT * FROM MyDB.asdfasdfasdf;"
ERROR 1146 (42S02) at line 1: Table 'MyDB.asdfasdfasdf;' doesn't exist
$ echo $?
1

所以你可以做的是执行查询,然后:

So what you can do is to execute the query and then:

if [ $? ... ]; then ...

这篇关于醒目退出code执行MySQL查询后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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