Linux 检查目录是否用 bash 挂载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9422461/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:46:46  来源:igfitidea点击:

Check if directory mounted with bash

linuxbashcentosmount

提问by Justin

I am using

我在用

mount -o bind /some/directory/here /foo/bar

I want to check /foo/barthough with a bash script, and see if its been mounted? If not, then call the above mount command, else do something else. How can I do this?

我想/foo/bar用 bash 脚本检查一下,看看它是否已安装?如果没有,则调用上面的挂载命令,否则执行其他操作。我怎样才能做到这一点?

CentOS is the operating system.

CentOS 是操作系统。

采纳答案by Christopher Neylan

Running the mountcommand without arguments will tell you the current mounts. From a shell script, you can check for the mount point with grepand an if-statement:

运行mount不带参数的命令将告诉您当前的安装。在 shell 脚本中,您可以grep使用 if 语句检查挂载点:

if mount | grep /mnt/md0 > /dev/null; then
    echo "yay"
else
    echo "nay"
fi

In my example, the if-statement is checking the exit code of grep, which indicates if there was a match. Since I don't want the output to be displayed when there is a match, I'm redirecting it to /dev/null.

在我的示例中,if 语句正在检查 的退出代码grep,该代码指示是否存在匹配。由于我不希望在匹配时显示输出,因此我将其重定向到/dev/null.

回答by Mark J. Bobak

You didn't bother to mention an O/S.

你懒得提O/S。

Ubuntu Linux 11.10 (and probably most up-to-date flavors of Linux) have the mountpointcommand.

Ubuntu Linux 11.10(可能还有最新的 Linux 版本)具有该mountpoint命令。

Here's an example on one of my servers:

这是我的一台服务器上的示例:

$ mountpoint /oracle
/oracle is a mountpoint
$ mountpoint /bin
/bin is not a mountpoint

Actually, in your case, you should be able to use the -qoption, like this:

实际上,就您而言,您应该可以使用该-q选项,如下所示:

mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar

Hope that helps.

希望有帮助。

回答by Hudson Santos

Another clean solution is like that:

另一个干净的解决方案是这样的:

$ mount | grep /dev/sdb1 > /dev/null && echo mounted || echo unmounted

For sure, 'echo something' can be substituted by whatever you need to do for each case.

当然,'echo something' 可以用你需要为每种情况做的任何事情来代替。

回答by Jordan Effinger

In my .bashrc, I made the following alias:

在我的 .bashrc 中,我做了以下别名:

alias disk-list="sudo fdisk -l"

回答by Jonathan H

The manual of mountpointsays that it:

的手册mountpoint说它:

checks whether the given directory or file is mentioned in the /proc/self/mountinfo file.

检查给定的目录或文件是否在 /proc/self/mountinfo 文件中提及。

The manual of mountsays that:

的手册mount说:

The listing mode is maintained for backward compatibility only. For more robust and customizable output use findmnt(8), especially in your scripts.

保留列表模式只是为了向后兼容。要获得更强大和可定制的输出,请使用 findmnt(8),尤其是在您的脚本中。

So the correct command to use is findmnt, which is itself part of the util-linuxpackage and, according to the manual:

因此,正确使用的命令是findmnt,它本身就是util-linux包的一部分,并且根据手册:

is able to search in /etc/fstab, /etc/mtab or /proc/self/mountinfo

能够在 /etc/fstab、/etc/mtab 或 /proc/self/mountinfo 中搜索

So it actually searches more things than mountpoint. It also provides the convenient option:

所以它实际上搜索的东西比mountpoint. 它还提供了方便的选项:

-M, --mountpoint path

Explicitly define the mountpoint file or directory. See also --target.

-M, --mountpoint路径

显式定义挂载点文件或目录。另见--target。

In summary, to check whether a directory is mounted with bash, you can use:

总之,要检查目录是否使用 bash 挂载,您可以使用:

if [[ $(findmnt -M "$FOLDER") ]]; then
    echo "Mounted"
else
    echo "Not mounted"
fi


Example:

例子:

mkdir -p /tmp/foo/{a,b}
cd /tmp/foo

sudo mount -o bind a b
touch a/file
ls b/ # should show file
rm -f b/file
ls a/ # should show nothing

[[ $(findmnt -M b) ]] && echo "Mounted"
sudo umount b
[[ $(findmnt -M b) ]] || echo "Unmounted"

回答by Tanky Woo

My solution:

我的解决方案:

is_mount() {
    path=$(readlink -f )
    grep -q "$path" /proc/mounts
}

Example:

例子:

is_mount /path/to/var/run/mydir/ || mount --bind /var/run/mydir/ /path/to/var/run/mydir/

For Mark J. Bobak's answer, mountpointnot work if mount with bindoption in different filesystem.

对于Mark J. Bobak 的回答mountpoint如果bind在不同的文件系统中使用选项挂载则不起作用。

For Christopher Neylan's answer, it's not need to redirect grep's output to /dev/null, just use grep -qinstead.

对于Christopher Neylan 的回答,不需要将 grep 的输出重定向到 /dev/null,只需使用即可grep -q

The most important, canonicalize the path by using readlink -f $mypath:

最重要的是,通过使用规范化路径readlink -f $mypath

  • If you check path such as /path/to/dir/end with backslash, the path in /proc/mountsor mountoutput is /path/to/dir
  • In most linux release, /var/run/is the symlink of /run/, so if you mount bind for /var/run/mypathand check if it mounted, it will display as /run/mypathin /proc/mounts.
  • 如果检查路径如/path/to/dir/以反斜杠结尾,则输入/proc/mountsmount输出的路径为/path/to/dir
  • 在大多数Linux发行版,/var/run/是符号链接/run/,因此,如果您安装了绑定/var/run/mypath,并检查它是否安装,它会显示为/run/mypath/proc/mounts

回答by Bruno Bronosky

I like the answers that use /proc/mounts, but I don't like doing a simple grep. That can give you false positives. What you reallywant to know is "do any of the rows have this exact string for field number 2". So, ask that question. (in this case I'm checking /opt)

我喜欢使用 的答案/proc/mounts,但我不喜欢做简单的 grep。这可能会给您带来误报。您真正想知道的是“是否有任何行具有字段编号 2 的确切字符串”。所以,问这个问题。(在这种情况下,我正在检查/opt

awk -v status=1 ' == "/opt" {status=0} END {exit status}' /proc/mounts

# and you can use it in and if like so:

if awk -v status=1 ' == "/opt" {status=0} END {exit status}' /proc/mounts; then
  echo "yes"
else
  echo "no"
fi

回答by Gabriel Fair

The answers here are too complicated just check if the mount exists using:

这里的答案太复杂了,只需使用以下方法检查安装是否存在:

cat /proc/mounts | tail -n 1

cat /proc/mounts | tail -n 1

This only outputs the last mounted folder, if you want to see all of them just remove the tail command.

这仅输出最后安装的文件夹,如果您想查看所有文件夹,只需删除 tail 命令。