Check to see if share is mounted

Polytropon freebsd at
Tue Oct 29 11:55:25 UTC 2013

On Tue, 29 Oct 2013 07:40:19 -0400, Jerry wrote:
> I have several Bash scripts that backup data to a Windows drive. I am
> looking for a one or two line snippet of code that I can use to
> determine if the share is all ready mounted. I tried trapping the exit
> code of the "mount_smbfs" command, but it always returns "0". Is there
> another way to get info on the share?

That's quite easy. Just query "df" and grep for the name of
the mountpoint or the CIFS name ("device field" as it appears
in /etc/fstab). Here's an example:

	if [ df | grep "/home/bob/stuff" ]; then
		... do stuff when share is mounted ...
		echo "CIFS share not availble."
		exit 1

Here, /home/bob/stuff would be the mountpoint. You could
also use the CIFS name, but it's probably more work to get
that name into a correct grep search pattern. Just make sure
the result of grep is _unique_. Of course, use variables to
make the thing more configurable. :-)

Of course, you could also query the mount command itself
and grep the results. Similarly - depending on your coding
preferences -, you could also do something like this:

	mount | grep "on /home/bob/stuff (" > /dev/null 2>&1
	[ $? -eq 1 ] && exit 1
	... and here continuing with the share mounted ...

The use of "on ... (" helps to avoid false-positive results.

Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...

More information about the freebsd-questions mailing list