logo
Published on Vision Multimedia Technologies, LLC (http://www.vmtllc.com)

Verifying a filesystem mount in a bash script

By rob.carlson
Created 07/17/2007 - 3:17am

Sometimes you need to check if a drive is properly mounted before performing a scripted operation.  A few Linux tools can detect whether they are on an NFS share or not, but there is no shorthand to find if a certain path is the mounted filesystem or an empty unmounted directory.

 

The easiest method appears to be using grep to evaluate the output of df or mount.  Instead of the lines containing the string, the quiet mode of grep (-q option) returns an error code representing whether the string was found.  This can be placed in a conditional inside a shell script like in the following example:

 

if df |grep -q '/mnt/mountpoint$'
        then
        echo "Found mount point, running task"
        # Do some stuff
        else
        echo "Aborted because the disk is not mounted"
        # Do some error correcting stuff
        exit -1
    fi

 

 


Source URL:
http://www.vmtllc.com/verifying-a-filesystem-mount-in-a-bash-script