0

I have this code

$db=test-1
for T in `mysql -u$dbUser -p$dbPass -N -B -e 'show tables from '$db`;
do
count=$((count+1))
mysqldump --skip-comments --compact --skip-lock-tables -u$dbUser -p$dbPass $db $T > $GIT_MYSQL/$T.sql
done
done;

It gives me this error

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-1' at line 1

How can I resolve it?

Krishna Mohan
  • 1,503
  • 3
  • 22
  • 28
rkevx21
  • 2,441
  • 5
  • 19
  • 40
  • 3
    There is a single quote `'` missing after `$db` – Alex Jan 25 '16 at 07:29
  • 2
    1). The Bash backtick syntax is deprecated, prone to typos and mis-reading, and cannot be nested. (And it's painful to put \` in SO comments). Consider using the `$(...)` syntax instead. 2). You generally should use double-quotes around parameter expansions to prevent word-splitting, etc. See [BashGuide](http://mywiki.wooledge.org/BashGuide/Practices#Quoting) for details. You may also find [ShellCheck](http://www.shellcheck.net) helpful. – PM 2Ring Jan 25 '16 at 08:18
  • and what do you mean by `$db=test-1` ? I would have expected `db=$(( test - 1))` or similar. Good luck. – shellter Jan 25 '16 at 13:55
  • $db="test-1" If test-1 is the name of your database and for T in `mysql -u$dbUser -p$dbPass -N -B -e 'show tables from '$db'`. Like Jaco say, there a ' missing. – Joao Vitorino Jan 25 '16 at 15:07

1 Answers1

2

You're not declaring your database name variable properly. Try doing this:

$db=test-1
echo "My database is called $db"

Compare the output with this:

db=test-1
echo "My database is called $db"

Taking into account comments above and proper (I hope) quoting, your script like this should work:

dbUser="user"
dbPass="pass"
db="test-1"
for T in $(mysql -u "$dbUser" -p"$dbPass" -N -B -e "show tables from $db")
do
    mysqldump --skip-comments --compact --skip-lock-tables -u "$dbUser" -p"$dbPass" "$db" "$T" > "$GIT_MYSQL/$T.sql"
done
miken32
  • 42,008
  • 16
  • 111
  • 154