16

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:

#!/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
fredrik
  • 9,631
  • 16
  • 72
  • 132

3 Answers3

28

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

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 mount command outputs in my machine. You said you use MacOS so it should work, but depending on what mount command outputs, you may need to modify the code above.

ysakamoto
  • 2,512
  • 1
  • 16
  • 22
  • 2
    instead of `> /dev/null` you can do `grep -q` – Aditya Nov 27 '17 at 21:01
  • On my imac (macos 10.13.6), it only works without ```> /dev/null``` or ```-q```... – Guntram Mar 10 '21 at 17:23
  • For me, this did not work as expected if you are chrooted because `mount` prepends the path to the chroot directory in the output but the argument given to `mountpoint` will not have that directory prepended. A dirty solution would be to remove the `on ` prefix in the grep search string. – mxmlnkn Aug 29 '21 at 20:58
4

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

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

For OS X 10.10 Yosemite I have to change to:

df | awk '{print $9}' | grep -Ex "/Volumes/myvolume"
daniel
  • 3,105
  • 4
  • 39
  • 48
  • 1
    Since it's the last field on the line that awk finds in both cases, you can use `$NF` instead of `$6` or `$9`. – Brian Cline Apr 11 '16 at 05:44
  • Good answer! As a minor niggle, I'd probably use `awk` for the match instead of `grep`, but the idea of parsing `df` is **key**. I wonder if this is reliable under all circumstances? On Catalina, `$9` is still the correct column (`Mounted on`), so a string match should do it?? I was hoping for something more like `findmnt` in Linux, but that's just not available in this post-ro filesystem world of mac os? –  Jun 07 '22 at 00:49
0

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

[ -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-line additional link about smb which I don't know well

# 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.

so I added mkdir $LOCALMOUNTPOINT

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