#!/bin/csh -f # get_external_drive_id # ------------------------------------------------------------------------- # Shell script to get the diskutil id of an external drive # ------------------------------------------------------------------------- # Usage: See Usage section below or run with -h or --help to see usage. # Assumptions: # Effects: # - Echos the drive id to stdout # Notes: # Implementation Notes: # Portability Issues: # Revision History: # $Log$ # ----------------------------------------------------------------------------- while ($#argv > 0) if ("$1" == "-h" || "$1" == "--help") then echo "Usage: $0:t [options] drive_name" echo "Options:" echo " -h = Show this help text" echo " --help = Show this help text" exit 1 else if ("-" == "`echo $1:q | cut -c 1`") then beep "Error: Invalid option: $1:q" $0 --help exit 1 else # Not a recognized option. Assume it's the first argument break endif end # Collect command line arguments if ($#argv < 1) then beep "Error: No drive_name specified." $0:t --help exit 1 else if ($#argv > 1) then beep "Error: Too many arguments specified." $0:t --help exit 1 endif set drive_name = "$1" # This technique copied from: # - https://egeek.me/2012/07/03/unmount-flash-drive-in-os-x/ diskutil list | awk 'BEGIN { last_disk="" } /^\/dev\/disk/ { last_disk=$1 } ($2 ~ /'"$drive_name"'/) || ($3 ~ /'"$drive_name"'/) { print last_disk; exit }'