bash 如何使用bash检查文件路径是否安装在OS X中?

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

How to check if filepath is mounted in OS X using bash?

macosbash

提问by fredrik

How do I perform the mount only if it has not already been mounted?

仅当尚未安装时如何执行安装?

This is on OS X 10.9 and this is what I have currently:

这是在 OS X 10.9 上,这是我目前拥有的:

#!/bin/bash

# Local mount point
LOCALMOUNTPOINT="/folder/share"

# Perform the mount if it does not already exist
if ...
then
/sbin/mount -t smbfs //user:password@serveraddress/share $LOCALMOUNTPOINT

else
    echo "Already mounted"
fi

回答by ysakamoto

While @hd1's answer gives you whether the file exists, it does not necessary mean that the directory is mounted or not. It is possible that the file happen to exist if you use this script for different machines or use different mount points. I would suggest this

虽然@hd1 的回答会告诉您该文件是否存在,但这并不意味着该目录是否已挂载。如果您将此脚本用于不同的机器或使用不同的挂载点,则该文件可能恰好存在。我会建议这个

LOCALMOUNTPOINT="/folder/share"

if mount | grep "on $LOCALMOUNTPOINT" > /dev/null; then
    echo "mounted"
else
    echo "not mounted"
fi

Note that I include "on" in grep statement based on what mountcommand outputs in my machine. You said you use MacOS so it should work, but depending on what mountcommand outputs, you may need to modify the code above.

请注意,我根据mount我机器中的命令输出在grep 语句中包含“on” 。你说你使用 MacOS 所以它应该可以工作,但根据mount命令输出,你可能需要修改上面的代码。

回答by daniel

This is what I use in my shell scripts on OS X 10.7.5

这是我在 OS X 10.7.5 上的 shell 脚本中使用的

df | awk '{print }' | grep -Ex "/Volumes/myvolume"

For OS X 10.10 Yosemite I have to change to:

对于 OS X 10.10 Yosemite,我必须更改为:

df | awk '{print }' | grep -Ex "/Volumes/myvolume"

回答by Jeongpyo Lee

For me from this solution in stackoverflow_QA_Check if a directory exists in a shell script

对于我从stackoverflow_QA_Check 中的这个解决方案来说,shell 脚本中是否存在目录

[ -d /Volumes/ ] && echo "Already mounted /Volumes/ in OS X" || echo "Not mounted"

to use that

使用那个

# 2 lines
$LOCALMOUNTPOINT="/folder/share" ;
[ -d $LOCALMOUNTPOINT ] && echo "Already mounted $LOCALMOUNTPOINT in OS X" || /sbin/mount -t smbfs //user:password@serveraddress/share $LOCALMOUNTPOINT

Here is Stackoverflow_QA_how-can-i-mount-an-smb-share-from-the-command-lineadditional link about smb which I don't know well

这是Stackoverflow_QA_how-can-i-mount-an-smb-share-from-the-command-line关于 smb 的附加链接,我不太了解

# Here is my solution
$LOCALMOUNTPOINT="/folder/share" ;
[ ! -d $LOCALMOUNTPOINT ] && mkdir $LOCALMOUNTPOINT && /sbin/mount -t smbfs //user:password@serveraddress/share $LOCALMOUNTPOINT || echo already mounted $LOCALMOUNTPOINT

I have to make new folder for my SMOOTH use before mounting that.

在安装之前,我必须为我的 SMOOTH 使用创建新文件夹。

so I added mkdir $LOCALMOUNTPOINT

所以我补充说 mkdir $LOCALMOUNTPOINT

Thank you I learned a lot with this case. Have A nice day!!! ;)

谢谢,我从这个案例中学到了很多。祝你今天过得愉快!!!;)