LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   syntax error near unexpected token `else' (http://www.linuxquestions.org/questions/programming-9/syntax-error-near-unexpected-token-%60else-742855/)

alex0 07-26-2009 03:32 AM

syntax error near unexpected token `else'
 
hey im alex ive been using linux(ubuntu now) for about 4 or 5 years now and ive recently been getting into bash scripts and have ran across a problem with this rsync script i made from here and there...

Code:

#!/bin/bash
# Lock file in the /tmp directory
LOCKFILE="/tmp/backup.lck"

# Place a log file in ?? and choose the date format
LOGFILE='/home/alex/bin/log/lock/back/'`date +%d-%m-%Y`'.txt'

# Append start time to the log file

date +[G\ O\ %H:%M] >> $LOGFILE

# Check wether we have a lock file, if so, don't execute
if [ -f $LOCKFILE ]; then
date +[N\ O\ %H:%M] && echo /tmp/backup.lck exists, aborting >> $LOGFILE
sleep 5; notify-send "Rsync" "Rsync is either already running or the lock is stuck!" -i /home/alex/bin/locked.png
exit 1
fi

# Create the lock file
/bin/touch $LOCKFILE
date +[M\ K\ %H:%M]'Lock Created' >> $LOGFILE

# Do your thing here
ogg123 /home/alex/bin/dialog-warning.ogg
notify-send "Rsync" "Backup Started" -i /home/alex/bin/rsync.png
rsync -av /mnt/200GB/Homework /media/120GB/backup/200GB
rsync -av /mnt/200GB/Misc /media/120GB/backup/200GB
rsync -av /mnt/200GB/Music /media/120GB/backup/200GB
rsync -av /mnt/200GB/Pictures /media/120GB/backup/200GB
rsync -av  --exclude='home/alex/.purple/custom_smiley' --exclude='/home/alex/.Xauthority' --exclude='home/alex/.mozilla/firefox-3.5/dh2i004w.default/Cache/' /home /media/120GB/backup
ogg123 /home/alex/bin/dialog-warning.ogg
sleep 3; notify-send "Rsync" "Backup Finished" -i /home/alex/bin/rsync.png

# Done, removing lock file
rm -f $LOCKFILE
if [ -f $LOCKFILE ]; then
else sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE
exit
rm -f $LOCKFILE
if [ -f $LOCKFILE ]; then
else sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE
exit
rm -f $LOCKFILE
if [ -f $LOCKFILE ]; then
else
  sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE
exit
rm -f $LOCKFILE
if [ -f $LOCKFILE ]; then
else
  sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE
exit
rm -f $LOCKFILE
if [ -f $LOCKFILE ]; then
sleep 5; notify-send "Rsync" "Lock File Wont Delete" -i /home/alex/bin/dialog-warning.png && date +[X\ X\ %H:%M] Rsync - Lock File Wont Delete >> $LOGFILE
exit

which returns...

Code:

$ /home/alex/bin/backup.sh

Audio Device:  Advanced Linux Sound Architecture (ALSA) output

Playing: /home/alex/bin/dialog-warning.ogg
Ogg Vorbis stream: 1 channel, 44100 Hz
                                                                               
Done.
sending incremental file list

sent 10188 bytes  received 58 bytes  20492.00 bytes/sec
total size is 793425369  speedup is 77437.57
sending incremental file list

sent 868029 bytes  received 2337 bytes  580244.00 bytes/sec
total size is 28075264059  speedup is 32256.85
sending incremental file list

sent 135941 bytes  received 165 bytes  90737.33 bytes/sec
total size is 15872919956  speedup is 116621.75
sending incremental file list

sent 21050 bytes  received 51 bytes  42202.00 bytes/sec
total size is 126688735  speedup is 6003.92
sending incremental file list
home/alex/
home/alex/.xsession-errors
home/alex/.cache/notify-osd.log
home/alex/.gconfd/
home/alex/.gconfd/saved_state
home/alex/.mozilla/firefox-3.5/dh2i004w.default/
home/alex/.mozilla/firefox-3.5/dh2i004w.default/sessionstore.js
home/alex/bin/
home/alex/bin/backup.sh
home/alex/bin/backup.sh~
home/alex/bin/log/lock/back/26-07-2009.txt

sent 976972 bytes  received 2137 bytes  652739.33 bytes/sec
total size is 2769633682  speedup is 2828.73

Audio Device:  Advanced Linux Sound Architecture (ALSA) output

Playing: /home/alex/bin/dialog-warning.ogg
Ogg Vorbis stream: 1 channel, 44100 Hz
                                                                               
Done.
/home/alex/bin/backup.sh: line 37: syntax error near unexpected token `else'
/home/alex/bin/backup.sh: line 37: `else sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE'

help anyone?? i know that the problem is with getting my else to
Quote:

sleep 5; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[U\ N\ %H:%M] >> $LOGFILE
exit

druuna 07-26-2009 03:38 AM

Hi,

There's nothig for the then part to do. It should be:

if [ .... ]
then
<some action>
else
<some other action>
fi


And _not_

if [ .... ]
then
else
<some action>


Hope this helps.

Sergei Steshenko 07-26-2009 03:45 AM

I am not at all a shell guru, but why sometimes you have 'else' as single item on line, and sometimes not ?

Anyway, from http://www.linuxconfig.org/Bash_scripting_Tutorial :

Quote:

9.1. Simple Bash if/else statement
Please note the spacing inside the [ and ] brackets! Without the spaces, it won't work!
Code:

#!/bin/bash
directory="./BashScripting"

# bash check if directory exists
if [ -d $directory ]; then
        echo "Directory exists"
else
        echo "Directory does not exists"
fi


, so try to stick only to this style.

colucix 07-26-2009 03:48 AM

If you want to take some action if the file does not exist, you can simply negate the expression:
Code:

if [ ! -f $LOCKFILE]
then
  <commands>
fi


gnashley 07-26-2009 03:50 AM

I count 5 missing 'fi'

alex0 07-26-2009 07:33 AM

Quote:

Originally Posted by druuna (Post 3620531)
Hi,

There's nothig for the then part to do. It should be:

if [ .... ]
then
<some action>
else
<some other action>
fi


And _not_

if [ .... ]
then
else
<some action>


Hope this helps.

ahh thanks that was really helpful! it works a treat now, im going to probobly make it triply more complicated haha :)

and one more thing does anyone have any idea how to do <script> -h/<script> --help etc. command?

druuna 07-26-2009 08:20 AM

Hi,

Quote:

does anyone have any idea how to do <script> -h/<script> --help etc. command?
You could use the getopt command:
1) Example 15-56. Using getopt to parse command-line options (Advanced bash guide)
2) Example 14-22. Using getopts to read the options/arguments passed to a script (Advanced bash guide)

Hope this helps.

alex0 07-28-2009 08:01 AM

that helped for my --help & -h :)

its done :)

Code:

#!/bin/bash
# Lock file in the /tmp directory
LOCKFILE="/tmp/backup.lck"

# Place a log file in ?? and choose the date format
LOGFILE='/home/alex/bin/log/Backup/lock/'`date +%d-%m-%Y`'.txt'

# Help

while [ ! -z "$1" ]
do
  case "$1" in
    -h) echo "Usage:
Output: [GO] - execution
        [NO] - already locked
        [MK] - lock is made
        [UN] - lock is removed
        [XX] - deadlocked!
        [EX] - exit
        [FI] - finished
        [DD] - de-deadlocked
      Launching: $0
          Are you sure you want to backup [Yy/Nn]?
          Then Rsync will back up files...
      flags: --help - this help prompt
            -h    - this help prompt
            -y    - auto accept [Yy/Nn]";;
    --help) echo "Usage:
Output: [GO] - execution
        [NO] - already locked
        [MK] - lock is made
        [UN] - lock is removed
        [XX] - deadlocked!
        [EX] - exit
        [FI] - finished
        [DD] - de-deadlocked
      Launching: $0
          Are you sure you want to backup [Yy/Nn]?
          Then Rsync will back up files...
      flags: --help - this help prompt
            -h    - this help prompt
            -y    - auto accept [Yy/Nn]";;
    *) break;;
  esac
  shift
exit
done
# Append start time to the log file

date +[GO\ %H:%M]execution >> $LOGFILE

# Check wether we have a lock file, if so, don't execute
if [ -f $LOCKFILE ]; then
# Checks if rsync is running
if ps ax | grep -v grep | grep rsync
then
# If yes then exits
date +[NO\ %H:%M]alreadylocked >> $LOGFILE
notify-send "Rsync" "Rsync is either already running or the lock is stuck!" -i /home/alex/bin/locked.png
date +[NO\ %H:%M]'/tmp/backup.lck exists, aborting!' >> /home/alex/Desktop/uhoh
date +[EX\ %H:%M]exit >> $LOGFILE
exit 1
else
# If not it the lock file will be removed and we will continue...
rm -f $LOCKFILE
if [ ! -f $LOCKFILE ]
then
date +[DD\ %H:%M]de-deadlocked >> $LOGFILE
sleep 3; notify-send "Rsync" "De-deadlocked!!!" -i /home/alex/bin/unlocked.png && date +[UN\ %H:%M]lockremoved >> $LOGFILE
else
sleep 3; notify-send "Rsync" "Lock File Wont Delete" -i /home/alex/bin/dialog-warning.png && date +[XX\ %H:%M]'deadlocked!' >> $LOGFILE
date +[XX\ %H:%M]'deadlocked!' >> /home/alex/Desktop/uhoh
date +[EX\ %H:%M]exit >> $LOGFILE
exit
fi
fi
fi
# Check for -y and if yes it will backup automatically!

if [ "$1" = "-y" ] ; then

##### Backup

# Create the lock file
/bin/touch $LOCKFILE
date +[MK\ %H:%M]lockcreated >> $LOGFILE

# Do your thing here
ogg123 /home/alex/bin/dialog-warning.ogg
notify-send "Rsync" "Backup Started" -i /home/alex/bin/rsync.png

# Stuff to backup

rsync -av /mnt/200GB/Homework /media/120GB/backup/200GB
rsync -av /mnt/200GB/Misc /media/120GB/backup/200GB
rsync -av /mnt/200GB/Music /media/120GB/backup/200GB
rsync -av /mnt/200GB/Pictures /media/120GB/backup/200GB
rsync -av  --exclude='home/alex/.purple/custom_smiley' --exclude='/home/alex/.Xauthority' --exclude='home/alex/.mozilla/firefox-3.5/dh2i004w.default/Cache/' /home /media/120GB/backup

date +[FI\ %H:%M]finished >> $LOGFILE

# End of stuff to backup

ogg123 /home/alex/bin/dialog-warning.ogg
sleep 1; notify-send "Rsync" "Backup Finished" -i /home/alex/bin/rsync.png

# Done, removing lock file
rm -f $LOCKFILE
if [ ! -f $LOCKFILE ]
then
sleep 3; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[UN\ %H:%M]lockremoved >> $LOGFILE
else
sleep 3; notify-send "Rsync" "Lock File Wont Delete" -i /home/alex/bin/dialog-warning.png && date +[XX\ %H:%M]'deadlocked!' >> $LOGFILE
date +[XX\ %H:%M]'deadlocked!' >> /home/alex/Desktop/uhoh
date +[EX\ %H:%M]exit >> $LOGFILE
exit
fi
fi
##### Backup end

# Ask if we want to backup

asksure() {
echo "Are you sure you want to backup [Yy/Nn]?"
while read -r -n 1 -s answer; do
  if [[ $answer = [YyNn] ]]; then
    [[ $answer = [Yy] ]] && retval=0
    [[ $answer = [Nn] ]] && retval=1
    break
  fi
done

return $retval
}
### using it
if ! asksure; then
date +[EX\ %H:%M]exit >> $LOGFILE
exit
else

##### Backup

# Create the lock file
/bin/touch $LOCKFILE
date +[MK\ %H:%M]lockcreated >> $LOGFILE

# Do your thing here
ogg123 /home/alex/bin/dialog-warning.ogg
notify-send "Rsync" "Backup Started" -i /home/alex/bin/rsync.png

# Stuff to backup

rsync -av /mnt/200GB/Homework /media/120GB/backup/200GB
rsync -av /mnt/200GB/Misc /media/120GB/backup/200GB
rsync -av /mnt/200GB/Music /media/120GB/backup/200GB
rsync -av /mnt/200GB/Pictures /media/120GB/backup/200GB
rsync -av  --exclude='home/alex/.purple/custom_smiley' --exclude='/home/alex/.Xauthority' --exclude='home/alex/.mozilla/firefox-3.5/dh2i004w.default/Cache/' /home /media/120GB/backup

date +[FI\ %H:%M]finished >> $LOGFILE

# End of stuff to backup

ogg123 /home/alex/bin/dialog-warning.ogg
sleep 1; notify-send "Rsync" "Backup Finished" -i /home/alex/bin/rsync.png

# Done, removing lock file
rm -f $LOCKFILE
if [ ! -f $LOCKFILE ]
then
sleep 3; notify-send "Rsync" "Lock File Deleted" -i /home/alex/bin/unlocked.png && date +[UN\ %H:%M]lockremoved >> $LOGFILE
else
sleep 3; notify-send "Rsync" "Lock File Wont Delete" -i /home/alex/bin/dialog-warning.png && date +[XX\ %H:%M]'deadlocked!' >> $LOGFILE
date +[XX\ %H:%M]'deadlocked!' >> /home/alex/Desktop/uhoh
fi
fi
date +[EX\ %H:%M]exit >> $LOGFILE
exit
##### Backup(and file) end



All times are GMT -5. The time now is 08:12 AM.