检查目录是否用bash挂载
我正在使用
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你没有费心提及操作系统。
Ubuntu Linux 11.10(可能还有大多数最新版本的 Linux)具有 mountpoint 命令。
这是我的一台服务器上的示例:
实际上,在您的情况下,您应该能够使用
-q
选项,如下所示: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:
Actually, in your case, you should be able to use the
-q
option, like this:运行不带参数的
mount
命令会告诉您当前的安装情况。在 shell 脚本中,您可以使用grep
和 if 语句检查挂载点:在我的示例中,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 withgrep
and an if-statement: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
.mountpoint 的手册说:
mount
的手册说:所以正确使用的命令是
findmnt
,它本身就是util-linux
包,并根据手册:因此它实际上比
mountpoint
搜索更多内容。它还提供了方便的选项:综上所述,要检查某个目录是否已使用 bash 挂载,可以使用:
示例:
The manual of
mountpoint
says that it:The manual of
mount
says that:So the correct command to use is
findmnt
, which is itself part of theutil-linux
package and, according to the manual:So it actually searches more things than
mountpoint
. It also provides the convenient option:In summary, to check whether a directory is mounted with bash, you can use:
Example:
我喜欢使用
/proc/mounts
的答案,但我不喜欢做简单的 grep。这可能会给您带来误报。您真正想知道的是“是否有任何行具有与字段 2 对应的确切字符串”。所以,问这个问题。 (在本例中我正在检查/opt
)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
)我的解决方案:
示例:
对于 Mark J. Bobak 的回答,如果使用 < 挂载,则
mountpoint
不起作用不同文件系统中的 code>bind 选项。对于 Christopher Neylan 的回答,不需要将 grep 的输出重定向到 /dev/null,只需使用
grep -q
相反。最重要的是,使用
readlink -f $mypath
规范化路径:/path/to/dir/
end 之类的路径使用反斜杠,/proc/mounts
或mount
输出中的路径为/path/to/dir
/ var/run/
是符号链接/run/
,因此如果您为/var/run/mypath
挂载绑定并检查它是否已挂载,它将显示为/run/mypath
> 在/proc/mounts
中。My solution:
Example:
For Mark J. Bobak's answer,
mountpoint
not work if mount withbind
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
:/path/to/dir/
end with backslash, the path in/proc/mounts
ormount
output is/path/to/dir
/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
.这里的答案太复杂了,只需使用以下命令检查挂载是否存在:
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.
另一个干净的解决方案是这样的:
当然,“回显某些内容”可以替换为您需要针对每种情况执行的操作。
Another clean solution is like that:
For sure, 'echo something' can be substituted by whatever you need to do for each case.
在我的 .bashrc 中,我创建了以下别名:
In my .bashrc, I made the following alias: