我需要为shell脚本中的所有mysql查询设置牵引力,所以我必须找到所有查询是否成功执行,所以我的简单代码如下。在这里我必须找到所有成功完成的mysql查询。
#!/bin/bash
set -x
date=$(date +"%Y")
month=$(date +"%m")
day=$(date +"%d")
user="appuser"
password="Appuser"
mysql=/usr/local/mysql/bin/mysql
db="finance"
tbname="cash_expense"
$mysql -u$user -p$password -S"/var/lib/mysql/mysql.sock" $db -N -e"create table new like $tbname;rename table $tbname to $tbname$date;rename table new to $tbname;truncate table $tbname;"
答案 0 :(得分:3)
你可以查看mysql的退出代码
演示:
~$ cat a.sh
#!/bin/sh
mysql -e"select 1"
ret=$?
echo "correct syntax: $ret"
mysql -e"select bad syntax"
ret=$?
echo "bad syntax: $ret"
if [ "$ret" = "0" ]; then
echo "mysql executed ok"
else
echo "mysql executed failed"
fi
~$ sh a.sh
+---+
| 1 |
+---+
| 1 |
+---+
correct syntax: 0
ERROR 1054 (42S22) at line 1: Unknown column 'bad' in 'field list'
bad syntax 1
mysql executed failed