Justin Justin - 1 year ago 88
Linux Question

Check if directory mounted with bash

I am using

mount -o bind /some/directory/here /foo/bar

I want to check
though with a bash script, and see if its been mounted? If not, then call the above mount command, else do something else. How can I do this?

CentOS is the operating system.

Answer Source

Running the mount command without arguments will tell you the current mounts. From a shell script, you can check for the mount point with grep and an if-statement:

if mount | grep /mnt/md0 > /dev/null; then
    echo "yay"
    echo "nay"

In my example, the if-statement is checking the exit code of grep, which indicates if there was a match. Since I don't want the output to be displayed when there is a match, I'm redirecting it to /dev/null.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download