ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
#!/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.