Monday, December 18, 2017

Bash Completion for Removing Harddrives Via the Command Line

If you're frequently adding and removing harddrives from your mac you might want to be able to do so from the command line. Here is a simple shell script (unharddisk) to allow detaching volumes.

If you're using bash completion there's a small source file (unharddisk.source_me) to include in your shell startup script (usually .bashrc). This file provides you with the command unhd with command completion.

In the given configuration I'm excluding the drive names MAINDRIVE and Sharky, as these are drives I never want to unmount. You can tweak the unharddisk.source_me file to your own needs:

COMPREPLY=( $( compgen -d /Volumes/$cur|cut -b 10-| egrep -v '(MAINDRIVE|Sharky)') )

If you don't want to exclude any drives from the completions, just remove the egrep command from this line entirely:

COMPREPLY=( $( compgen -d /Volumes/$cur|cut -b 10-) )

Or if you want to only skip one drive e.g. by the name of MyHD:

COMPREPLY=( $( compgen -d /Volumes/$cur|cut -b 10-| egrep -v 'MyHD') )

Thanks to Annika Backstrom for her concise bash completion example.

unharddisk.zip

No comments:

Post a Comment