Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A bash script to mount and unmount volumes UNIX
The following is a bash shell script, that I've named 'disk', that when run, and passed a volume name, will attempt to mount that volume if unmounted, and will unmount the volume if presently mounted. It's a convenience wrapper for MacOS X's 'diskutil' utility.

#!/bin/bash
if [ -z $1 ] ; then
    echo "usage : disk <diskname>"
    echo "Mounts <diskname> if it's not mounted, and"
    echo "unmounts it if it is already mounted."
    exit 1
fi
NAME=$1
PART=`diskutil list|grep $NAME|awk '{print $6}'`

if [ -z `ls -1 /Volumes/ | grep $NAME` ] ; then 
    # check that PART appears to be a disk partition
    echo Checking $NAME $PART
    if [ `file /dev/$PART | awk '{print $2}'` = "block" ] ; then
  echo Mounting $NAME $PART
     diskutil mount /dev/$PART
    else
       echo /dev/$PART does not appear to be a disk partition - exiting
  exit 1
    fi
else
    echo unmounting $NAME
    diskutil unmount /Volumes/$NAME
fi
[robg adds: I haven't tested this one myself...]
    •    
  • Currently 3.00 / 5
  You rated: 5 / 5 (4 votes cast)
 
[20,139 views]  

A bash script to mount and unmount volumes | 3 comments | Create New Account
Click here to return to the 'A bash script to mount and unmount volumes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A bash script to mount and unmount volumes
Authored by: Titanium Man on Mar 12, '03 12:03:29AM

Nice script. I've been using the following for a while now to eject external drives, CDs, etc.

eject () {
/usr/sbin/disktool -e $(df | grep $1 | awk '{print $1}' | sed 's/\/dev\///')
}

You type 'eject /Volumes/name', and then it ejects.



[ Reply to This | # ]
A bash script to mount and unmount volumes
Authored by: rodbrim on Feb 25, '04 05:44:34PM

You can always do it like this from a shell session. ssh into your machine and do it remotely (if you are comfortable turning on remote login)
osascript -e 'tell application "Finder"' -e 'eject <volume name>' -e 'end tell'



[ Reply to This | # ]
The script also mounts!
Authored by: mlm on Mar 27, '04 04:51:58PM

These eject scripts -- and the technologies they use -- only eject; the bash script originally posted also mounts. I have a large but whiny Firewire drive I use only for my iTunes library. I like to mount it and dismount it according to whether I'm using it with iTunes or not. There are various little utilities out there for unmounting disks, but I don't think I've seen one that remounts and unmounted one. And I use the shell a lot, so this is perfect for what I want.

In fact, I was stuck for a name when I put this script in a file on my machine: it mounts the volume if unmounted, and unmounts the volume if mounted, so it "toggles" the mount status of the disk, but "toggle" is a bit weak a word here. I tried "switchmount", but too long and "flipmount", but too hokey. Ended up with "switchvol", which is OK, but not great -- suggestions?

Actually, it doesn't really matter what I called it because I made a shell alias whose name was the same as the disk, so now I can mount/unmount my iTunes library disk with just a one-word command.

Thanks for working this out. There are a lot of ways to get at volume information, and it isn't always easy to find what you want in a reasonable form. The approach here is direct (maybe even crude), but it would be difficult to improve on it. I could use Mount Me! (or something like it) to mount my disk and DocJector (or something like) to eject it, which I do sometimes, but when I know which disk I want to mount/unmount, and I switch frequently I'd be just as happy to have a direct shell script.

---
--- Mitchell



[ Reply to This | # ]