Bash for 循环:反引号
Bash for loop: Backticks
我有这个代码
$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;
它给我这个错误
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
我该如何解决?
您没有正确声明数据库名称变量。尝试这样做:
$db=test-1
echo "My database is called $db"
将输出与此进行比较:
db=test-1
echo "My database is called $db"
考虑到上面的评论和适当的(我希望)引用,你的脚本应该可以工作:
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
我有这个代码
$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;
它给我这个错误
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
我该如何解决?
您没有正确声明数据库名称变量。尝试这样做:
$db=test-1
echo "My database is called $db"
将输出与此进行比较:
db=test-1
echo "My database is called $db"
考虑到上面的评论和适当的(我希望)引用,你的脚本应该可以工作:
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