我有一个查询mysql的bash脚本,我想抓住错误并回复它。这是我的代码
mysql --silent -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${DATABASE} -BNe "$sql_str" | while read -r line
do
query="$(echo "$line" | cut -f1)"
database_out="$(echo "$line" | cut -f2)"
outfile="$(echo "$line" | cut -f3)"
directory_out="$(echo "$line" | cut -f4)"
type="$(echo "$line" | cut -f5)"
host="$(echo "$line" | cut -f6)"
username="$(echo "$line" | cut -f7)"
directory_scp="$(echo "$line" | cut -f8)"
#switch over format types
case "$type" in
csv)
file_out=$directory_out$outfile"."`date +%Y%m%d`.csv
mysql -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${database_out} -e "$query" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > $file_out
;;
xml)
file_out=$directory_out$outfile`date +%Y%m%d`.xml
mysql -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${database_out} -e "$query" -X > $file_out
;;
*)
echo "["$(timestamp)"] : {ERROR} Invalid output format. Currently only csv and xml output supported."
;;
esac
str=$file_out" "$username@$host:$directory_scp
echo "["$(timestamp)"] : {NOTIFICATION} Attempting to scp "$outfile" to "$username@$host:$directory_scp" - [Started]"
scp $str
echo "["$(timestamp)"] : {NOTIFICATION} scp "$outfile" to "$username@$host:$directory_scp" - [Complete]"
done
任何想法。我想捕获错误并格式化错误输出。
答案 0 :(得分:0)
result=$(mysql --silent -h ${sql_server} -P 3306 -u${USER} -p${MYSQL_PASS} -D${DATABASE} -BNe "$sql_str")
if [ $? -ne "0" ]; then
echo "["$(timestamp)"] : {ERROR} MySQL error thrown in EOD DUMP"
else
while read -r line
...do something