i am using a script in my macbook pro to backup Mysql databases of my development apps, and its working fine.
But i tried to use it in my ubuntu server test and gave this error :
Syntax error: "(" unexpected (expecting "done")
On this line of code:
table_types=($($MYSQL -u $DB_USER -p$DB_PASS -e
"show table status from $DB" | awk '{ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
Can someone help me?
You forgot to open a bracket "(". Try this:
table_types=($($MYSQL -u $DB_USER -p$DB_PASS -e "show table status from $DB" | awk '({ if ($2 == "MyISAM" || $2 == "InnoDB") print $1,$2}'))
Related
I have a mysql procedure which I want to run from a shell script in linux and store the output in a log file. But my script is not working. Below is my script:
#!/bin/bash
source CX20-PIM-properties.prop
status=$(mysql -u $user -h $host -D $database -se "call fetchFromPAsIsToPIDX()")
if [ $status ]; then
echo "Procedure executed successfully" | tee procedure_output.log
else
echo "Procedure execution failed" | tee procedure_output.log
fi
And the output is below:
[anurag#pimdev0 ~]$ ./load-from-AsIs-to-IDX.sh
Procedure execution failed
You should store the return value of cmd in status not stdout of cmd.
#!/bin/bash
source CX20-PIM-properties.prop
if [ `mysql -u $user -h $host -D $database -se "call fetchFromPAsIsToPIDX()" &> /dev/null` ]; then
echo "Procedure executed successfully" | tee procedure_output.log
else
echo "Procedure execution failed" | tee procedure_output.log
fi
I have a bash script that queries mysql and I would like to catch the error and echo it . Here is my code
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
Any ideas. I would like to catch error and format the error output.
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
I have the following script:
if [ `mysql -u root -p < test.sql` ]; then
echo "success"
else
echo "some error"
fi
How can I check in the if if the command went well or there was some sql error?
You don't need the test command, i.e. [ here.
Assuming that mysql produces a non-zero exit code in case of failure and 0 on success, then you can say:
if mysql -u root -p < test.sql; then
echo "success"
else
echo "some error"
fi
Another way this is typically done is with the "&&" and "||" separators. For example:
mysql -u root -p < test.sql && echo "success" || echo "some error"
If the mysql command exits with status 0, then "success" will be printed. If it doesn't, "some error" will be printed.
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
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;
Any ideas
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 ...
I created a little script which must change engine for tables:
#!/bin/bash
echo "use test_1;" > query.sql
get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;" >> query.sql
}
get_list ;
mysql -u teamcity -ppassword < query.sql
But - how can I avoid use query.sql file? I make few attempts with "Here document" - but can't solve it...
For example - trying this:
#!/bin/bash
get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}
a="$(get_list)"
b="use test_1;"
c="$b $a"
mysql -u teamcity -ppassword <<< $c
But it is not working...
Put everything in a function and pipe that:
#!/bin/bash
get_list () {
echo "use test_1;"
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}
get_list | mysql -u teamcity -ppassword