
さて、私はなぜbashが私にこのようなエラーを投げているのかわかりません。ドライブがディレクトリにマウントされていることを確認するスクリプトを作成しました。ドライブがマウントされたら、いくつかのrsync操作を実行し、ステータスをログに出力します。インストールされていない場合は、私に電子メールを送信する必要があります(コードで編集されています)。
ただし、このコードを実行するたびに、「構文エラー: 'else'の近くに予期しないトークンがあります」というメッセージが表示されます。この構文エラーはなぜ発生しますか?
1 [ en 2 [[ および f 文を使用して sudo でスクリプトを実行しようとしましたが、サイコロは使用しませんでした。
ロジックを表示できるように、コードにコメントを追加しました。
#!/bin/bash
#Print to log that check is starting
printf "Checking if Backup drive is successfully mounted\n" >>/home/fileserver/Applications/Backup/logs/backup.log 2>&1 &&
# Start check
if [[ $(mount | grep -c /home/fileserver/Backup4TB) != 0 ]]; then
# If check is successfull, print it to log & start the backup
printf "Backup Drive successfully mounted, Backing up Applications folder to USB Backup Drive\n" >>/home/fileserver/Applications/Backup/logs/backup.log 2>&1 &&
# Backup using rsync
rsync --log-file=/home/fileserver/Applications/Backup/logs/rsync.log -avhP --delete /home/fileserver/Applications/ /home/fileserver/Backup4TB/Applications >/dev/null 2>&1 &&
# Print to log
printf "Backing up Books folder to USB Backup Drive\n" >>/home/fileserver/Applications/Backup/logs/backup.log 2>&1 &&
# Backup using rsync
rsync --log-file=/home/fileserver/Applications/Backup/logs/rsync.log -avhP --delete /home/fileserver/Media/Books/ /home/fileserver/Backup4TB/Books >/dev/null 2>&1 &&
# SYNTAX ERROR IS HERE - If check is unsuccessfull
else
# Print error to log
printf "ERROR - Mount was insuccesfull, sending email as warning\n" >>/home/fileserver/Applications/Backup/logs/backup.log 2>&1 &&
# Email me
/usr/sbin/ssmtp "MYEMAIL" < /home/fileserver/Applications/Backup/mountingerror/mountingerrorBackup.txt
fi
答え1
はい、見つかりました。最後のコマンドの後、else文の前に&&を入れました。
...
# Backup using rsync
rsync --log-file=/home/fileserver/Applications/Backup/logs/rsync.log -avhP --delete /home/fileserver/Media/Books/ /home/fileserver/Backup4TB/Books >/dev/null 2>&1 &&
# SYNTAX ERROR IS HERE - If check is unsuccessfull
else
...
&& を削除するとエラーが消えます。
...
# Backup using rsync
rsync --log-file=/home/fileserver/Applications/Backup/logs/rsync.log -avhP --delete /home/fileserver/Media/Books/ /home/fileserver/Backup4TB/Books >/dev/null 2>&1
# SYNTAX ERROR IS HERE - If check is unsuccessfull
else
...