Notifications are for KDE, later I'll upload script with notifications for Gnome3. Notifications are using program notify-send included in the package notify-osd or libnotify-bin.
The script umnounts the first partition of the device.USAGE: umountdev sda, this will unmount the sda (sda1) drive.
How it works:
First the variables are initialized. one for the device (sda) the other for the first partition (sda1). Next into the ARRAY is stored the device name, if the device doesn't have a name, the default one is used (/dev/sda).
Next the script will find out if the 1. partition is able to be umnounted, if it is used, the notification is send with the list of processes using the device.
In the following condition it is tested if the device exists and if it can be unmounted.
Notifications are shown when:
- the device doesn't exist
- the device exists, but it can't be unmounted
- the device exists, but is already unmounted
NOTE: the script uses the eject command, therefore you need permissions for this command (e.g. setting up sudoers). notify-send uses icons stored in my home folder under ~/.icons; the default location for the icons is in KDE /usr/share/icons (optionally /opt/kde3/share/icons).
The script:
#!/bin/bash
dev=$1'1'
device=$1
#echo $dev
ARRAY=( `cd /dev/disk/by-label; ls -l * | grep $dev | sed -e
's/\://'` )
if [ "${ARRAY[7]}" == "" ]; then
ARRAY[7]="'/dev/$device'"
fi
if [ -b /dev/$dev ]; then
lsof=`lsof -t /dev/$dev | head -n 1`
if [ $lsof ];then
notify-send "The device ${ARRAY[7]} is being used by:" \ "`lsof
/dev/$dev | grep -v COMMAND`" -t 9000 -i
~/.icons/messagebox_warning.png
exit 0
fi
fi
if [ -b /dev/$device ]; then
lsof=`lsof -t /dev/$device | head -n 1`
if [ $lsof ];then
notify-send "The device ${ARRAY[7]} is being used by:" \ "`lsof
/dev/$device | grep -v COMMAND`" -t 9000 -i
~/.icons/messagebox_warning.png
else
sudo eject /dev/$device
notify-send "The device ${ARRAY[7]} was unmounted" \ "You can now
remove the device." -t 9000 -i ~/.icons/button_ok.png
fi
else
notify-send "The device \"$device\" does not exist." \ "It has been
probably unmounted earlier." -t 9000 -i
~/.icons/messagebox_info.png
fi
exit 0
______________________________________________________________________________________EDIT: I changed the script, so that all partitions of the device are umnounting now.
Usage stayed the same: umountdev sda
#!/bin/bash
### ### ###
#KrisKo 2010#
### ### ###
#variable indicating if device is used
ERR=0
devpath=/dev/
#List all mounted partitions into the array
MOUNTED=( `mount | grep -o "$1[1-9]"` )
for DEV in ${MOUNTED[*]}; do
#find out the name of mounted partition and use default when no name set
ALIAS=( "`cd /dev/disk/by-label; ls -l * | grep $DEV | cut -d " " -f 8 | sed 's/\\\x20/ /g'`" )
if [ "$ALIAS" == "" ]; then
ALIAS="$devpath$DEV"
fi
#check if the partition is being used, if not, eject the device
lsof=`lsof -t $devpath$DEV | head -n 1`
if [ $lsof ];then
notify-send "The device \"$ALIAS\" is being used by:" \ "`lsof $devpath$DEV | grep -v COMMAND`" -i ~/.icons/messagebox_warning.png
ERR=1
else sudo eject $devpath$DEV
fi
done
#exit on error (exits if one or more devices are being used)
if [ $ERR == 1 ]; then
exit 1
fi
if [ -b $devpath$1 ]; then
lsof=`lsof -t $devpath$1 | head -n 1`
if [ $lsof ];then
notify-send "The device \"$devpath$1\" is being used by:" \ "`lsof $devpath$1 | grep -v COMMAND`" -i ~/.icons/messagebox_warning.png
else
sudo eject $devpath$1
notify-send "The device \"$devpath$1\" was unmounted" \ "You can now remove the device." -i ~/.icons/button_ok.png
fi
else
notify-send "The device \"$devpath$1\" does not exist." -i ~/.icons/messagebox_info.png
fi
exit 0
No comments:
Post a Comment