I am coding a script to check mysql databases using bash script, I would like to check if the status of a table is not 'OK', will return the table name and do some more actions:
check.log
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
check.sh
# check and repair tables mysqlcheck -u $hostUser -p$hostPasswd --all-databases --check --auto-repair >> check.log
nCheck=$(awk '$2 != "OK" {print $1}' repairDBsF.log)
echo 'c '$nCheck
// this will return a result in one like:
// table2 table5 table7 table8
Is there anyway to have a result like:
Number of damaged tables are 4
table2 (Some error here)
table5 (Another error)
table7 (Broken)
table8 (A very long error which take a 2 lines of multiple errors)
Yes, correct, I am only concerned by the page display and the number of errors:
Number of damaged tables are 4 table2 (Some error here) table5 (Another error) table7 (Broken) table8 (A very long error which take a 2 lines of multiple errors)
---------- Post updated at 01:14 AM ---------- Previous update was at 01:08 AM ----------
@Scrutinizer: could explain the code for me please?
@RudiC: I am looking for a bash code, if you could explain more please
awk '
$2!="OK" { # if the second field is not equal to OK
t++ # increase total
$2="(" $2 # prepend opening parenthesis to the second field
# and by assigning a value to one of the fields:
# recalculate $0 (which automatically removes excess spacing)
$NF=$NF ")" # append closing parenthesis to last field
s=s $0 "\n" # create one string "s" that contains all the information separated
# by newlines
}
END { # After all lines are processes, print the total and the string
printf "The number of damaged tables is %s\n\n%s",t,s
}
' file