A bash script to mount and unmount volumes

Mar 11, '03 09:18:00AM

Contributed by: mhenders

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...]

Comments (3)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030307112511721