检查目录是否用bash挂载

发布于 2025-01-08 14:47:49 字数 197 浏览 1 评论 0原文

我正在使用

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

我想使用 bash 脚本检查 /foo/bar ,并查看它是否已安装?如果没有,则调用上面的 mount 命令,否则执行其他操作。我该怎么做?

CentOS是操作系统。

I am using

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

I want to check /foo/bar though 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?

CentOS is the operating system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

拥醉 2025-01-15 14:47:50

你没有费心提及操作系统。

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

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

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

实际上,在您的情况下,您应该能够使用 -q 选项,如下所示:

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

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

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

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 -q option, like this:

mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar
以往的大感动 2025-01-15 14:47:50

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

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

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

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

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.

冷弦 2025-01-15 14:47:50

mountpoint 的手册说:

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

mount 的手册说:

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

所以正确使用的命令是findmnt,它本身就是util-linux 包,并根据手册:

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

因此它实际上比 mountpoint 搜索更多内容。它还提供了方便的选项:

-M, --mountpoint 路径

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

综上所述,要检查某个目录是否已使用 bash 挂载,可以使用:

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

示例:

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"

The manual of mountpoint says that it:

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

The manual of mount says that:

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

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

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

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

-M, --mountpoint path

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

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

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"
陌路黄昏 2025-01-15 14:47:50

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

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

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

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

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 really want 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)

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

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

if awk -v status=1 '$2 == "/opt" {status=0} END {exit status}' /proc/mounts; then
  echo "yes"
else
  echo "no"
fi
梦忆晨望 2025-01-15 14:47:50

我的解决方案:

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

示例:

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

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

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

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

  • 如果您检查诸如 /path/to/dir/ end 之类的路径使用反斜杠,/proc/mountsmount 输出中的路径为 /path/to/dir
  • 在大多数 Linux 版本中,/ var/run/ 是符号链接/run/,因此如果您为 /var/run/mypath 挂载绑定并检查它是否已挂载,它将显示为 /run/mypath > 在 /proc/mounts 中。

My solution:

is_mount() {
    path=$(readlink -f $1)
    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, mountpoint not work if mount with bind option in different filesystem.

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

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

  • If you check path such as /path/to/dir/ end with backslash, the path in /proc/mounts or mount output is /path/to/dir
  • In most linux release, /var/run/ is the symlink of /run/, so if you mount bind for /var/run/mypath and check if it mounted, it will display as /run/mypath in /proc/mounts.
羁绊已千年 2025-01-15 14:47:50

这里的答案太复杂了,只需使用以下命令检查挂载是否存在:

cat /proc/mounts | tail -n 1

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

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

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.

夏日落 2025-01-15 14:47:50

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

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

当然,“回显某些内容”可以替换为您需要针对每种情况执行的操作。

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.

也只是曾经 2025-01-15 14:47:50

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

alias disk-list="sudo fdisk -l"

In my .bashrc, I made the following alias:

alias disk-list="sudo fdisk -l"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文