Bash: 无法解决意外的标记语法错误
Bash: Couldn't resolve the unexpected token syntax error
目标: 我正在尝试设置 VVV 我不打算在特定数据库上默认 运行 数据库备份。
我被卡住的地方: 即使我在 bash 脚本中使用换行符,我仍然得到 syntax error near unexpected token `done'
。
采取的步骤: 我还确保我在两个 bash 脚本中都有 LF 行结尾,但我得到了同样的错误。
文件:vagrant_halt_custom
#!/bin/bash
#
# This script is run whenever `vagrant halt` is used to power off
# the virtual machine. To customize this behavior, include a file
# in your local VVV/config/homebin directory: vagrant_halt_custom
#
# Look for a custom trigger file. If this exists, we'll assume that
# all trigger actions should be handled by this custom script. If
# it does not exist, then we'll handle some basic tasks.
db_backup_custom
文件:db_backup_custom
#!/bin/bash
#
# Create individual SQL files for each database. These files
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
mysql -e 'show databases' | \
grep -v -F "information_schema" | \
grep -v -F "performance_schema" | \
grep -v -F "mysql" | \
grep -v -F "test" | \
grep -v -F "Database" | \
while read dbname; do if [ "$dbname" == "mydb" ]; then echo "Database $dbname skipped." && continue fi; mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql && echo "Database $dbname backed up..."; done
错误
==> default: Running triggers before halt...
/home/vagrant/bin/db_backup_custom: line 12: syntax error near unexpected token `done'
/home/vagrant/bin/db_backup_custom: line 12: `while read dbname; do if [ "$dbname" == "mydb" ]; then echo "Database $dbname skipped." && continue fi; mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql && echo "Database $dbname backed up..."; done'
Connection to 127.0.0.1 closed.
==> default: Attempting graceful shutdown of VM...
==> default: [vagrant-hostsupdater] Removing hosts
continue
后少了一个分号。
有什么理由不将代码格式化为更多行?
while read dbname; do
if [ "$dbname" == "mydb" ] ; then
echo "Database $dbname skipped."
&& continue
fi
mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql &&
echo "Database $dbname backed up..."
done
顺便说一句,不需要 |
后的反斜杠,因为命令不能以单独的竖线结尾。
经过多次尝试和错误,我得出了以下答案。
文件:db_backup_custom
#!/bin/bash
#
# Create individual SQL files for each database. These files
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
mysql -e 'show databases' | \
grep -v -F "information_schema" | \
grep -v -F "performance_schema" | \
grep -v -F "mysql" | \
grep -v -F "test" | \
grep -v -F "Database" | \
while read dbname; do
if [ "$dbname" == "mydb" ] ; then
echo "Database $dbname skipped." \
&& continue;
fi
mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql &&
echo "Database $dbname backed up..."; done;
提示:使用以下命令验证您的bash脚本是否有任何语法错误。
bash -n bin/db_backup_custom
目标: 我正在尝试设置 VVV 我不打算在特定数据库上默认 运行 数据库备份。
我被卡住的地方: 即使我在 bash 脚本中使用换行符,我仍然得到 syntax error near unexpected token `done'
。
采取的步骤: 我还确保我在两个 bash 脚本中都有 LF 行结尾,但我得到了同样的错误。
文件:vagrant_halt_custom
#!/bin/bash
#
# This script is run whenever `vagrant halt` is used to power off
# the virtual machine. To customize this behavior, include a file
# in your local VVV/config/homebin directory: vagrant_halt_custom
#
# Look for a custom trigger file. If this exists, we'll assume that
# all trigger actions should be handled by this custom script. If
# it does not exist, then we'll handle some basic tasks.
db_backup_custom
文件:db_backup_custom
#!/bin/bash
#
# Create individual SQL files for each database. These files
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
mysql -e 'show databases' | \
grep -v -F "information_schema" | \
grep -v -F "performance_schema" | \
grep -v -F "mysql" | \
grep -v -F "test" | \
grep -v -F "Database" | \
while read dbname; do if [ "$dbname" == "mydb" ]; then echo "Database $dbname skipped." && continue fi; mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql && echo "Database $dbname backed up..."; done
错误
==> default: Running triggers before halt...
/home/vagrant/bin/db_backup_custom: line 12: syntax error near unexpected token `done'
/home/vagrant/bin/db_backup_custom: line 12: `while read dbname; do if [ "$dbname" == "mydb" ]; then echo "Database $dbname skipped." && continue fi; mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql && echo "Database $dbname backed up..."; done'
Connection to 127.0.0.1 closed.
==> default: Attempting graceful shutdown of VM...
==> default: [vagrant-hostsupdater] Removing hosts
continue
后少了一个分号。
有什么理由不将代码格式化为更多行?
while read dbname; do
if [ "$dbname" == "mydb" ] ; then
echo "Database $dbname skipped."
&& continue
fi
mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql &&
echo "Database $dbname backed up..."
done
顺便说一句,不需要 |
后的反斜杠,因为命令不能以单独的竖线结尾。
经过多次尝试和错误,我得出了以下答案。
文件:db_backup_custom
#!/bin/bash
#
# Create individual SQL files for each database. These files
# are imported automatically during an initial provision if
# the databases exist per the import-sql.sh process.
mysql -e 'show databases' | \
grep -v -F "information_schema" | \
grep -v -F "performance_schema" | \
grep -v -F "mysql" | \
grep -v -F "test" | \
grep -v -F "Database" | \
while read dbname; do
if [ "$dbname" == "mydb" ] ; then
echo "Database $dbname skipped." \
&& continue;
fi
mysqldump -uroot "$dbname" > /srv/database/backups/"$dbnme".sql &&
echo "Database $dbname backed up..."; done;
提示:使用以下命令验证您的bash脚本是否有任何语法错误。
bash -n bin/db_backup_custom