Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?
Here is a variant with "df -P" which is supposed to be portable:
mat@owiowi:/tmp$ f(){ df -P | awk '{ if($6 == "'$1'")print }' ; }
mat@owiowi:/tmp$ f /
/dev/mapper/lvm0-vol1 20642428 17141492 2452360 88% /
mat@owiowi:/tmp$ f /mnt
mat@owiowi:/tmp$ f /mnt/media
/dev/mapper/lvm0-media 41954040 34509868 7444172 83% /mnt/media
stat --printf '%m' shows the mount point of a given file or directory.
realpath converts relative paths to direct.
Comparing the results of the two will tell you if a directory is a mount point. stat is very portable. realpath is less so, but it is only needed if you want to check relative paths.
I'm not sure how portable mountpoint is.
if [ "$(stat --printf '%m' "${DIR}")" = "$(realpath "${DIR}")" ]; then
echo "This directory is a mount point."
else
echo "This is not a mount point."
fi
Without realpath:
if [ "${DIR}" = "$(stat --printf '%m' "${DIR}")" ]; then
echo "This directory is a mount point."
else
echo "This is not a mount point."
fi
I discover that on my Fedora 7 there is a mountpoint command.
From man mountpoint:
NAME
mountpoint - see if a directory is a mountpoint
SYNOPSIS
/bin/mountpoint [-q] [-d] /path/to/directory
/bin/mountpoint -x /dev/device
Apparently it come with the sysvinit package, I don't know if this command is available on other systems.
[root@myhost~]# rpm -qf $(which mountpoint)
sysvinit-2.86-17
df $path_in_question | grep " $path_in_question$"
This will set $?
upon completion.
for mountedPath in `mount | cut -d ' ' -f 3`; do
if [ "${mountedPath}" == "${wantedPath}" ]; then
exit 0
fi
done
exit 1
Unfortunately both mountpoint and stat will have the side-effect of MOUNTING the directory you are testing if you are using automount. Or at least it does for me on Debian using auto cifs to a WD MyBookLive networked disk. I ended up with a variant of the /proc/mounts made more complex because each POTENTIAL mount is already in /proc/mounts even if its not actually mounted!
cut -d ' ' -f 1 < /proc/mounts | grep -q '^//disk/Public$' && umount /tmp/cifs/disk/Public
Where 'disk' is the name of the server (networked disk) in /etc/hosts. '//disk/Public' is the cifs share name '/tmp/cifs' is where my automounts go (I have /tmp as RAM disk and / is read-only) '/tmp/cifs/disk' is a normal directory created when the server (called 'disk') is live. '/tmp/cifs/disk/Public' is the mount point for my 'Public' share.