How to check if file path is set in OS X using bash?

How to mount only if it is not already installed?

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

#!/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 
+7
bash macos
source share
2 answers

While @ hd1's answer gives you if the file exists, it does not necessarily mean that the directory is installed or not. Perhaps the file exists 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 the grep statement based on the mount command that appears on my computer. You said you were using MacOS, so it should work, but depending on what mount commands print, you may need to change the code above.

+12
source share

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

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

For OS X 10.10 Yosemite I need to change to:

 df | awk '{print $9}' | grep -Ex "/Volumes/myvolume" 
+4
source share

All Articles