How to test if a given path is a mount point

后端 未结 10 823
-上瘾入骨i
-上瘾入骨i 2021-01-31 08:41

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

10条回答
  •  失恋的感觉
    2021-01-31 09:09

    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.
    

提交回复
热议问题