LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-26-2009, 03:32 AM   #1
alex0
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Rep: Reputation: 0
Question 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
 
Old 07-26-2009, 03:38 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 07-26-2009, 03:45 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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.
 
Old 07-26-2009, 03:48 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
Old 07-26-2009, 03:50 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,765

Rep: Reputation: 473Reputation: 473Reputation: 473Reputation: 473Reputation: 473
I count 5 missing 'fi'
 
Old 07-26-2009, 07:33 AM   #6
alex0
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by druuna View Post
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?
 
Old 07-26-2009, 08:20 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
Old 07-28-2009, 08:01 AM   #8
alex0
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

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
 
  


Reply

Tags
bash, else, help, rsync


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Syntax error near unexpected token fi Nortekman Linux - Newbie 8 02-27-2009 03:48 PM
syntax error near unexpected token Aigarzs Linux - Newbie 3 01-12-2008 03:39 PM
syntax error near unexpected token deskjockey39 Linux - General 2 01-16-2007 08:47 AM
syntax error for unexpected token `(' Steve Spurr Linux - Newbie 6 09-22-2006 08:19 AM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM


All times are GMT -5. The time now is 09:40 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration