bash 如何测试给定路径是否是挂载点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if a given path is a mount point
提问by Andrea Francia
Suppose do you want test if /mnt/disk is a mount point in a shell script. How do you do this?
假设您想测试 /mnt/disk 是否是 shell 脚本中的挂载点。你怎么做到这一点?
回答by Andrea Francia
I discover that on my Fedora 7 there is a mountpoint command.
我发现在我的 Fedora 7 上有一个 mountpoint 命令。
From man mountpoint:
从 man 挂载点:
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.
显然它是随 sysvinit 包一起提供的,我不知道该命令在其他系统上是否可用。
[root@myhost~]# rpm -qf $(which mountpoint)
sysvinit-2.86-17
回答by ephemient
Not relying on mount
, /etc/mtab
, /proc/mounts
, etc.:
不依靠mount
,/etc/mtab
,/proc/mounts
,等:
if [ `stat -c%d "$dir"` != `stat -c%d "$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.
当$dir
是挂载点时,它的设备编号与其父目录不同。
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
.
到目前为止列出的替代方案的好处是您不必解析任何内容,并且如果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.
缺点是它没有标记/
为挂载点。嗯,这对于特殊情况很容易,但仍然如此。
回答by ephemient
Using GNU find
使用 GNU 查找
find <directory> -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
缺点:使用 GNU find 所以不太便携
Advantages: Reports mount points not recorded in /etc/mtab.
优点:报告未记录在 /etc/mtab 中的挂载点。
回答by Howard
Unfortunately both mountpoint and stat will have the side-effect of MOUNTINGthe 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 POTENTIALmount is already in /proc/mounts even if its not actually mounted!
不幸的是这两个安装点和统计将有副作用MOUNTING你,如果你正在使用自动安装测试的目录。或者至少它对我在 Debian 上使用自动 cifs 到 WD MyBookLive 网络磁盘有帮助。我最终得到了一个更复杂的 /proc/mounts 变体,因为每个潜在的mounts 已经在 /proc/mounts 中,即使它没有实际安装!
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.
回答by Howard
df $path_in_question | grep " $path_in_question$"
This will set $?
upon completion.
这将$?
在完成后设置。
回答by haggai_e
if mount | cut -d ' ' -f 3 | grep '^/mnt/disk$' > /dev/null ; then
...
fi
EDIT: Used Bombe's idea to use cut.
编辑:使用 Bombe 的想法来使用 cut。
回答by Bombe
for mountedPath in `mount | cut -d ' ' -f 3`; do
if [ "${mountedPath}" == "${wantedPath}" ]; then
exit 0
fi
done
exit 1
回答by MatthieuP
Here is a variant with "df -P" which is supposed to be portable:
这是一个带有“df -P”的变体,它应该是可移植的:
mat@owiowi:/tmp$ f(){ df -P | awk '{ if( == "''")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
回答by MatthieuP
mount | awk ' == "/pa/th" {print }'
Empty if is not a mountpoint ^^
如果不是挂载点则为空 ^^
回答by Pedro
stat --printf '%m'shows the mount point of a given file or directory.
stat --printf '%m'显示给定文件或目录的挂载点。
realpathconverts relative paths to direct.
realpath将相对路径转换为直接路径。
Comparing the results of the two will tell you if a directory is a mount point. statis very portable. realpathis less so, but it is only needed if you want to check relative paths.
比较两者的结果会告诉你一个目录是否是一个挂载点。 stat非常便携。 realpath不那么重要,但只有在您想检查相对路径时才需要它。
I'm not sure how portable mountpointis.
我不确定便携式挂载点如何。
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