In a bash script, I do mounting and unmounting of a folder. I have the script send messages to my desktop via notify-send. I wish to trim this output slightly and have my code robust enough to notify me of the condition.
The relevant variables in the script:
In this instance, the backup folder is mounted and the current script requests it to unmount via this command:
The output in the notification looks like:
which is what I want when it works.
Note that there is no error condition checking, so it reports "backup unmounted" regardless whether it actually worked or not.
The last couple of days, I had left a console window open at the backup folder, so it did not actually unmount it The script redirects the errors to a log file, so I could see what had happened. I decide to "upgrade" my script to tell my directly when this occurs.
The replacement code for the above code snippet:
Output notification:
1. This does what I want except it's doing the same thing twice. It seems like I could shorten the code a bit by not having to separately create the message content via running the umount command twice.
2. The notification is a bit long. I'd like to trim "Backup unmount failed. /mnt/root_backup: target is busy" to "Backup unmount failed: target is busy" but I can't seem to be able to figure out how to sed or grep my way there.
The relevant variables in the script:
Code:
backup_folder="/mnt/root_backup/" dismount="true" NOTIFYUSER="myuser" NOTIFYDISPLAY=":0.0"
Code:
# Unmount backup folder if requested." if [ "$dismount" = true ] ; then umount "$backup_folder" su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmounted.'" echo "Backup unmounted" fi
The output in the notification looks like:
Daily Backup:
Backup unmounted.
Backup unmounted.
Note that there is no error condition checking, so it reports "backup unmounted" regardless whether it actually worked or not.
The last couple of days, I had left a console window open at the backup folder, so it did not actually unmount it The script redirects the errors to a log file, so I could see what had happened. I decide to "upgrade" my script to tell my directly when this occurs.
The replacement code for the above code snippet:
Code:
# Unmount backup folder if requested." if [ "$dismount" = true ] ; then umount "$backup_folder" fi # Check to see if unmount worked if [[ -d "$backup_folder" ]] then # umount did not work nodismount=$((umount "$backup_folder" 2>&1) | grep -oP '(?<=umount: ).*') su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmount failed. $nodismount'" else # umount worked su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily Backup:' 'Backup unmounted.'" fi ##Script complete. exit 0
Daily Backup:
Backup unmount failed. /mnt/root_backup: target is busy.
Backup unmount failed. /mnt/root_backup: target is busy.
2. The notification is a bit long. I'd like to trim "Backup unmount failed. /mnt/root_backup: target is busy" to "Backup unmount failed: target is busy" but I can't seem to be able to figure out how to sed or grep my way there.