How to test if a given path is a mount point

后端 未结 10 820
-上瘾入骨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 08:58

    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
    

提交回复
热议问题