Sfoglia il codice sorgente

Find the device by name -- apparently the drive numbering for drutil is not stable

Zachary Kotlarek 13 anni fa
parent
commit
6bc5c9c54c
1 ha cambiato i file con 33 aggiunte e 10 eliminazioni
  1. 33 10
      discToMkv.sh

+ 33 - 10
discToMkv.sh

@@ -1,8 +1,7 @@
 #!/bin/bash
 
 # Config
-DRIVE_NUM=1
-DR_DRIVE_NUM=$(( $DRIVE_NUM + 1 ))
+DRIVE_NAME="iHBS112"
 OUT_DIR="${HOME}/Desktop/Docs/Downloads"
 APP_PATH="/Applications/Zach/Media/MakeMKV.app"
 
@@ -16,15 +15,42 @@ if [ ! -d "${OUT_DIR}" ]; then
 	exit 1
 fi
 
+# Find the DR drive number based on the device name
+i=1
+declare -i DRIVE_NUM_DR
+while [ $i -lt 100 ]; do
+	STATUS="`drutil -drive $i status 2>&1`"
+	if echo "${STATUS}" | grep -q "${DRIVE_NAME}"; then
+		DRIVE_NUM_DR=$i
+		break
+	elif echo "${STATUS}" | grep -q 'Could not find a valid device.'; then
+		break
+	fi
+	i=$(( $i + 1 ))
+done
+
 # Ensure a disk is inserted
-if drutil -drive "${DR_DRIVE_NUM}" status | grep -q 'Type: No Media Inserted'; then
+if drutil -drive "${DRIVE_NUM_DR}" status | grep -q 'Type: No Media Inserted'; then
 	echo "No disk available" 1>&2
-	drutil -drive "${DR_DRIVE_NUM}" tray open
+	drutil -drive "${DRIVE_NUM_DR}" tray open
+	exit 1
+fi
+
+# Find the MKV drive number and OS device path based on the device name
+declare -i DRIVE_NUM_MKV
+DEV_PATH=""
+DRIVE="`"${BIN_PATH}" --robot info dev | grep '^DRV:' | cut -d ':' -f 2- | grep "${DRIVE_NAME}"`"
+DRIVE_NUM_MKV="`echo "${DRIVE}" | awk -F ',' '{print $1}'`"
+DEV_PATH="`echo "${DRIVE}" | awk -F ',' '{print $7}' | cut -d '"' -f 2`"
+
+# Sanity check
+if [ $DRIVE_NUM_MKV -lt 1 ] || [ $DRIVE_NUM_DR -lt 1 ] || [ -z "${DEV_PATH}" ] || [ ! -c "${DEV_PATH}" ]; then
+	echo "Unable to find drive: ${DRIVE_NAME}" 1>&2
 	exit 1
 fi
 
 # Parse the disc
-"${BIN_PATH}" --noscan --robot "--messages=${TMP}" info "disc:${DRIVE_NUM}"
+"${BIN_PATH}" --noscan --robot "--messages=${TMP}" info "disc:${DRIVE_NUM_MKV}"
 
 # Find the disk title -- long if available, short if not
 CINFO="`grep '^CINFO\:' "${TMP}"`"
@@ -53,7 +79,7 @@ fi
 # Tracks are selected with the default profile (as set in the GUI)
 # Recommended selection string: -sel:all,+sel:(favlang|nolang),-sel:(core),+sel:special,-sel:mvcvideo,=100:all,-10:favlang
 # This preserves all audio and subtitles in your prefered language, all audio and subtitles with no language, and all special tracks, but excludes the core audio from DTS-HD tracks
-"${BIN_PATH}" --noscan --robot mkv "disc:${DRIVE_NUM}" all "${OUT_DIR}/${NAME}"
+"${BIN_PATH}" --noscan --robot mkv "disc:${DRIVE_NUM_MKV}" all "${OUT_DIR}/${NAME}"
 
 # Bail on error
 if [ $? -ne 0 ]; then
@@ -61,7 +87,4 @@ if [ $? -ne 0 ]; then
 fi
 
 # Eject when complete
-DISK="`drutil -drive "${DR_DRIVE_NUM}" status | awk '$3 == "Name:" {print $4}'`"
-if [ -n "${DISK}" ]; then
-	diskutil eject "${DISK}"
-fi
+diskutil eject "${DEV_PATH}"