tags:

views:

2022

answers:

11

Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?

A: 

You could just grep the path to see if it exists in /etc/mtab

MrWiggles
+2  A: 
for mountedPath in `mount | cut -d ' ' -f 3`; do
    if [ "${mountedPath}" == "${wantedPath}" ]; then
        exit 0
    fi
done
exit 1
Bombe
+1  A: 
if mount | cut -d ' ' -f 3 | grep '^/mnt/disk$' > /dev/null ; then
   ...
fi

EDIT: Used Bombe's idea to use cut.

haggai_e
This will fail if the mount path contains spaces
phihag
You can replace the `mount` command with `cat /proc/mounts` which escapes spaces to avoid this problem.
haggai_e
@haggai_e: I hope you meant replace mount with a `< /proc/mounts` redirect... avoid useless cat suffering!
Dave C
Another completely worthless optimization: use 'grep -q' instead of '>/dev/null'
Adam Rosenfield
Thanks, I guess these are just bad habit that are hard for me to quit :)
haggai_e
+3  A: 
TESTDIR=/mnt/disk

if grep -q "^[^ ]* ${TESTDIR} " /etc/mtab; then
        echo mounted
else
        echo not mounted
fi
phihag
An extra benefit from using /etc/mtab instead of running the mount command is that the mount command might get stuck. Mostly when network filesystems are used (like nfs, smb).
Tom Feiner
/etc/mntab (as well as output from mount(8)) often has nothing at all to do with what is currently mounted, and can give completely wrong answers. Use at your own risk.
Employed Russian
+2  A: 

To check if it's mounted:

mount | awk '{ print $1 }' | grep /dir

To check if it's mountable:

cat /etc/fstab | egrep -v '^\s*($|[#])' | awk '{ print $2 }' | grep /dir
Quassnoi
+1  A: 

Using GNU find
find -maxdepth 0 -printf "%D" will give the device number of the directory. If it differs between the directory and its parent then you have a mount point.

Add /. onto the directory name if you want symlinks to different filesystems to count as mountpoints (you'll always want it for the parent).

Disadvantages: uses GNU find so less portable

Advantages: Reports mount points not recorded in /etc/mtab.

A: 

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
MatthieuP
+5  A: 

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
Andrea Francia
The solutions that parse output /etc/mtab can provide the wrong answer: root file system could be mounted read-only, and /etc/mtab may not have been updated since last boot. mount(8) often simply prints the content of /etc/mtab, and will give wrong answer for the same reason.
Employed Russian
A: 

How about:

$df . | grep " $PWD$"

will set $? upon completion

Oops, having re-read the question:

df $path_in_question | grep " $path_in_question$"

~chaz

+1  A: 

Not relying on mount, /etc/mtab, /proc/mounts, etc.:

if [ `stat -fc%t:%T "$dir"` != `stat -fc%t:%T "$dir/.."` ]; then
    echo "$dir is mounted"
else
    echo "$dir is not mounted"
fi

When $dir is a mount point, it has a different device number than its parent directory.

The benefit over the alternatives listed so far is that you don't have to parse anything, and it does the right thing if dir=/some//path/../with///extra/components.

The downside is that it doesn't mark / as a mountpoint. Well, that's easy enough to special-case, but still.

ephemient
A: 

I'm writing a simple script for unmounting file systems on a hard drive, off a eSATA hard drive dock. file systems i'm unmounting are ext3, bind mounts, encfs, and bindfs fuse-mounts. my old motherboard doesn't have eSATA ports nor has BIOS support for eSATA, I'm using an eSATA bracket with eSATA cable. The current gnome nautilus doesn't detect and automount nor eject my eSATA device from its gui.

i've tried the stat -fc%t:%T command and it doesn't work on my linux mint 8 x86_64 box.

mountpoint command and find -maxdepth 0 -printf "%D" both have problem seeing non-public fuse mount points created by my encfs mounts.

find -maxdepth 0 -printf "%D" and df -P aren't seeing my bind mount points (or fuse bindfs mount points), and I would need to umount all of these to spin-down and undock the hard drive.

this is messy, for bind mount points i have the mountpoint command, for detecting ext2 mount points, i can use find -maxdepth 0 -printf "%D" or the mountpoint command. for unmounting private encfs and bindfs fuse mounts from root, it leaves me greping for /etc/mtab or outputs from mount command.