用于了解文件系统是否已安装的Shell脚本

nit*_*ian 17 filesystems shell mount

我有一个安装在特定目录上的tmpfs文件系统.我想编写一个shell脚本来检查tmpfs文件系统是否已经挂载在目录中.

Sor*_*gal 43

有一个专门用于此的工具:mountpoint(1)

if mountpoint -q "$directory" ; then
    echo it is a mounted mountpoint
else
    echo it is not a mounted mountpoint
fi
Run Code Online (Sandbox Code Playgroud)

你甚至不需要刮掉它就行了!

请注意,我在Debian的initscripts包中找到了这个工具.它在其他地方的可用性不是我可以评论的东西.

  • @cvolny:事实证明`mountpoint`是util-linux的一部分(特别是sys-utils部分),在大多数Linux发行版中可能都是这样或那样的.但是,它不是GNU的一部分,也不可​​能在非Linux系统上使用. (2认同)

cdh*_*wie 5

像这样的东西,虽然有点黑客,但应该可以解决问题:

FS_TO_CHECK="/dev" # For example... change this to suit your needs.

if grep -F " $FS_TO_CHECK " /proc/mounts > /dev/null; then
    # Filesystem is mounted
else
    # Filesystem is not mounted
fi
Run Code Online (Sandbox Code Playgroud)


eph*_*ent 5

您可以检查文件系统的类型。

$ stat -f -c'%T'/
xfs
$ stat -f -c'%T'/ dev / shm
tmpfs

您还可以通过将目录的设备与其父目录的设备进行比较来检查目录是否为挂载点。

$ stat -c'%D'/
901
$ stat -c'%D'/ home
fe01
$ stat -c'%D'/ home / $ USER
fe01