1

Allright, I absolutely do not understand why bash is throwing this error at me. I created a script that checks if a drive is mounted at a directory. If a drive is mounted, it does some rsync tasks (and prints status to a log). If it isn't mounted, it should email me (mail redacted from code).

But whenever I run this code, it throws me with a "Syntax error: unexpected token near 'else'". Why is this syntax error happening?

I tried with 1 [ en 2 [[ with the f statement, ran script under sudo, but no dice.

Extra comments added to the code so you can see the logic ;).

#!/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

closed as off-topic by Eric Renouf, Stéphane Chazelas, Jeff Schaller, GAD3R, Thomas Dickey Sep 26 '16 at 23:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – Eric Renouf, Stéphane Chazelas, Jeff Schaller, GAD3R, Thomas Dickey
If this question can be reworded to fit the rules in the help center, please edit the question.

3

Yeah, found it: I place && after my last command right before the else statement:

...

# 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

...

By removing the &&, the error disappears:

...

# 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

...

Not the answer you're looking for? Browse other questions tagged or ask your own question.