Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

if (disk is mounted) unmount if (disk is unmounted) mount

Shell Programming and Scripting

Thread Tools Search this Thread Display Modes
Old Unix and Linux 03-11-2011   -   Original Discussion by hollister
hollister's Unix or Linux Image
hollister hollister is offline
Registered User
Join Date: Mar 2011
Last Activity: 12 March 2011, 10:22 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question if (disk is mounted) unmount if (disk is unmounted) mount

Hey there, sorry if this is a bit too much of a noob question, trying to get to grips with a simple bash script - but i have done ZERO bash scripting.

basically having worked out how to mount and unmount disks using:
disktool -m *device* & disktool -e *device*

- and looking at the result of disktool -l
i see that from the results each device has a parameter Mountpoint = '/Volumes/My Disk' if the "My Disk" is mounted - or - Mountpoint = '' if unmounted.

Basically I am looking to write a script that looks to see if a partition on my external disk is mounted or not, if it is mounted - then the disk is unmounted - - and if it is unmounted the disk is mounted.

So this this disk is disk1s2. How do I query the Mountpoint and see what the result is?

Basically :

if (disk1s2.Mountpoint == "") {
disktool -m disk1s2

if (disk1s2.Mountpoint == "/Volumes/My Disk") {
disktool -e disk1s2

^ is obviously very, very, very wrong for a bash script - but you get the gist.

Any help would be greatly appreciated
Sponsored Links
Old Unix and Linux 03-11-2011   -   Original Discussion by hollister
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
Join Date: Feb 2011
Last Activity: 18 June 2018, 9:55 AM EDT
Posts: 350
Thanks: 16
Thanked 97 Times in 92 Posts
This works on Ubuntu Linux

rootId=$(stat -c%d /)
mountId=$(stat -c%d "${mountPart}")
if (( rootId == mountId ))
   # code for not mounted
   # code for mounted

Your milage may vary.

Sponsored Links
Old Unix and Linux 03-11-2011   -   Original Discussion by hollister
mirni's Unix or Linux Image
mirni mirni is offline
Registered User
Join Date: Mar 2011
Last Activity: 15 August 2015, 3:18 AM EDT
Posts: 686
Thanks: 51
Thanked 178 Times in 171 Posts
I don't know what is 'disktool'. It's not installed on my CentOS box, therefore I'd like to present a more general solution:
When a filesystem gets mounted, there is a record about it in /etc/mtab.
Each partition of your disk is gonna be represented by a special file /dev/sdb1 or /dev/sdc4, etc.. (sd* for SATA drives, at least).
Now this can change, depending on what devices you have currently plugged in, so it's not good to depend that your disk is always gonna be /dev/sdb1. A reliable way to find out which /dev/sd* is your disk is to find out the UUID of your disk:
Unmount and unplug it and do

ls -l /dev/disk/by-uuid

Then plug it in and issue the same command. You'll see one more entry (my disk is 886C3A4E6C3A3772), that's the uuid of your device. ls -l also shows you that it is a symbolic link to that special file /dev/sd*.
Now let's put all this info together:


device=`ls /dev/disk/by-uuid/886C3A4E6C3A3772 -l | awk -F/ '{print $NF}'`  #find which file in /dev/ is my disk

if [ _"`grep /dev/$device /etc/mtab`" = _ ] ; then
    mounted=0 # there is no record of this device in /etc/mtab
    echo "Device /dev/$device is not mounted"
    mount  /dev/$device /my/mountpoint
    mounted=1 # record found => mounted
    echo "Device /dev/$device is mounted"
    umount  /dev/$device

You may need root privileges to do the mounting though.
The disadvantage of this script is that you have to hardcode the UUID of your device in it. To abstract from this, you could
find the device by looking at the last device plugged in. Like this:

device=`ls /dev/disk/by-uuid -ltr | tail -1 | awk -F/ '{print $NF}'`

ls -trl will print the files in long format (-l), sorted by date (-t), in reverse order (-r), so you'll have the last plugged-in device last. tail -1 will grab just the last line of the output of ls command.

Hope this helps.

mounted=1 is just a flag in case you wanted to do some more bash magic outside the if statement
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unmount USB disk Guccio Shell Programming and Scripting 3 05-08-2010 01:34 PM
Kill shell script when host program not running/disk unmounted pcwiz Shell Programming and Scripting 2 10-02-2009 10:04 AM
When a disk won't 'Unmount" unimachead OS X (Apple) 3 07-12-2009 02:23 AM
Can't Mount Disk / Image after bad unmount Cranie OS X (Apple) 1 06-13-2009 06:19 PM
/usr out of disk space need to unmount/ expand volume /mount Dirk_ HP-UX 5 04-28-2009 08:35 AM

All times are GMT -4. The time now is 03:06 PM.