2017-11-28 101 views
0

目的:私はVVVを設定しようとしていますが、特定のデータベースでDBバックアップを実行しないようにしています。Bash:予期しないトークン構文エラーを解決できませんでした

私はスタックしています:私は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 

答えて

0

数多くの試行錯誤の末、次のような回答がありました。

ファイル: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 
3

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 

ところで、|後のバックスラッシュは必要ありません。

+0

私はあなたの答えを少し修正してみました。私はコメントにそれを掲示することができなかったので、別の答えとして投稿しました。しかし、大変ありがとう、私の一日を救った! –

+0

"コードをより多くの行に書式設定しない理由は何ですか?" - コードはVVVレポに属し、そのようにフォーマットされています。だから私は彼らの基準に固執した。 –