检查目录是否用 bash 挂载 [英] Check if directory mounted with bash
问题描述
我正在使用
mount -o bind /some/directory/here /foo/bar
我想用 bash 脚本检查 /foo/bar
,看看它是否已经挂载?如果不是,则调用上面的 mount 命令,否则执行其他操作.我该怎么做?
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 是操作系统.
CentOS is the operating system.
推荐答案
运行不带参数的 mount
命令将告诉您当前的安装.在 shell 脚本中,您可以使用 grep
和 if 语句检查挂载点:
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
在我的示例中,if 语句正在检查 grep
的退出代码,它指示是否存在匹配项.由于我不希望在匹配时显示输出,因此我将其重定向到 /dev/null
.
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
.
这篇关于检查目录是否用 bash 挂载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!