Alan W. Smith
Alan W. Smith

Reputation: 25445

How can I determine the volume name of CD/DVD in bash?

I'm trying to figure out how to get a bash script to automatically determine the path to a CD/DVD in order to process it. Running a Mac (10.7.4) the disk shows up at:

/Volumes/[Volume_name]

Since the volume name changes depending on the disk, I'm having to input that part manually. The operating system obviously knows it's a CD/DVD because of the way the controls work. Is it possible for bash to use whatever the OS uses to determine there is a CD/DVD and provide the path to it?

Upvotes: 3

Views: 3994

Answers (4)

Mark Reed
Mark Reed

Reputation: 95252

Putting together the pieces from above, I think this will do what you want:

get_cd_volume() { 
  local rc=1
  for disk in $(diskutil list | grep ^/); do 
    if diskutil info "$disk" | grep -q Optical; then
      df | sed -ne  "s,^$disk.*\(/Volumes.*\)$,\1,p"
      rc=0 
    fi
  done
  if (( rc )); then
    echo >&2 "No volume mounted."
  fi
  return $rc
}

Upvotes: 0

jackjr300
jackjr300

Reputation: 7191

I use drutil.

drutil uses the DiscRecording framework to interact with attached burning devices.

#!/bin/bash
id=$(drutil status |grep -m1 -o '/dev/disk[0-9]*')
if [ -z "$id" ]; then
    echo "No Media Inserted" 
else 
    df | grep "$id" |grep -o /Volumes.*
fi

Upvotes: 5

Jason T. Miller
Jason T. Miller

Reputation: 672

Given a UNIX block device name, diskutil info's output is easier to parse than mount's. For instance, this

function get_disk_mountpoint () {
    diskutil info $1 | perl -ne 'print "$1\n" if /^   Mount Point: +(.*)/';
}

works. Trouble is, OS X also dynamically assigns /dev/disk? devices to removable media, so you still need something like

function get_optical_mountpoints () {
    for i in $(diskutil list | egrep ^/); do
        if diskutil info $i | egrep -q '^   Optical Drive Type:' ; then
            get_disk_mountpoint $i
        fi
    done
}

to list the mount points for optical drives specifically.

Upvotes: 1

Martin
Martin

Reputation: 6687

If a disc is mounted you can use mount to view where it's mounted.

Upvotes: 0

Related Questions