简体   繁体   English

如何在bash中确定CD / DVD的卷名?

[英]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. 我正在试图弄清楚如何让bash脚本自动确定CD / DVD的路径以便处理它。 Running a Mac (10.7.4) the disk shows up at: 运行Mac(10.7.4)磁盘显示在:

/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. 由于控件的工作方式,操作系统显然知道它是CD / DVD。 Is it possible for bash to use whatever the OS uses to determine there is a CD/DVD and provide the path to it? bash是否可以使用操作系统使用的任何东西来确定CD / DVD并提供它的路径?

I use drutil . 我用drutil

drutil uses the DiscRecording framework to interact with attached burning devices. drutil使用DiscRecording框架与附加的刻录设备进行交互。

#!/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

如果安装了光盘,您可以使用mount来查看它的安装位置。

Given a UNIX block device name, diskutil info 's output is easier to parse than mount 's. 给定UNIX块设备名称, diskutil info的输出比mount更容易解析。 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? 麻烦的是,OS X还动态分配/ 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. 列出光驱的安装点。

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM