Skip to content

Instantly share code, notes, and snippets.

@heftig
Created April 14, 2012 16:28
Show Gist options
  • Save heftig/2385599 to your computer and use it in GitHub Desktop.
Save heftig/2385599 to your computer and use it in GitHub Desktop.
device_from_filename() {
# canonicalize filename
local realpath="$(readlink -e "$1")"
[[ ! -e $realpath ]] && return 1
# read /etc/mtab into associative array mtab
local -A mtab
local device mountpoint rest
while read device mountpoint rest; do
mtab[$mountpoint]="$device"
done </etc/mtab
# find mountpoint
while [[ -n $realpath && -z ${mtab[$realpath]} ]]; do
realpath="${realpath%/*}"
done
[[ -z $realpath ]] && realpath=/
echo ${mtab[$realpath]}
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment