Abu*_*ane 1 mysql command-line bash scripts text-processing
我正在编写一个脚本来使用 bash 脚本检查 mysql 数据库,我想检查表的状态是否不是“OK”,将返回表名并执行更多操作:
检查日志
table1 OK
table2 Some error here
table3 OK
table4 OK
table5 Another error
table6 OK
table7 Broken
table8 A very long error which take a 2 lines
of multiple errors
Run Code Online (Sandbox Code Playgroud)
检查.sh
# check and repair tables
mysqlcheck -u $hostUser -p$hostPasswd --all-databases --check --auto-repair >> check.log
# grep tables status, should return OK
cat check.log | grep 'OK' | awk '{print $2}'| while read status; do
# if any table does not contain OK
if [ "$status" != "OK" ]
then
##### HERE WHERE I am stuck
#### It loops and return all the table
# grep to get the table name
cat check.log | grep 'OK' | awk '{print $1}'| while read table; do
echo $tblName' is BROKEN'
done
else
echo $tblName' is FINE'
fi
done
// here I need to mail the tables that have problems in one single mail
Run Code Online (Sandbox Code Playgroud)
寻找像这样的输出:
tableName: error
tableName: error
table2: Some error here
table5: Another error
and so on
Run Code Online (Sandbox Code Playgroud)
这些行将输出到日志文件 (result.txt)
谢谢您的支持
您不需要使用 bash 逐行读取文件。awk 已经做到了。例如,您可以使用此代码打印损坏的条目
awk '$2!="OK"{print $0}' check.log
Run Code Online (Sandbox Code Playgroud)
您也可以只打印表名并使用该命令创建一个数组,然后对它们进行操作:
array=( $(awk '$2!="OK"{printf "%s ",$1 }' check.log) )
Run Code Online (Sandbox Code Playgroud)
注意旁边的空格%s,这很重要。
您还提到,该错误可以是多行的,就像 table8 一样。我们可能想避免弄乱它。因此,我们还可以为字段 1 添加一个检查以包含一个单词表。
$> array=( $(awk '$1~/table/ && $2!="OK"{printf "%s ",$1 }' check.log) )
$> echo ${array[@]}
table2 table5 table7 table8
$> echo ${array[0]}
table2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7233 次 |
| 最近记录: |