Search code examples
linuxbashcentosmount

Check if directory mounted with bash


I am using

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

I want to check /foo/bar 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.


Solution

  • 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"
    else
        echo "nay"
    fi
    

    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.