The way to Test if a Quantity is Mounted in Bash


If it’s essential to examine if a quantity is mounted in a Bash script, then you are able to do the next.

The way to Test Mounted Volumes

First we have to decide the command that may be capable to examine.

This may be completed with the /proc/mounts path.

The way to Test if a Quantity is Mounted in Bash

if grep -qs '/mnt/foo ' /proc/mounts; then
    echo "It is mounted."
else
    echo "It isn't mounted."
fi

The way to Test if a Quantity is Mounted and Obtainable

MNT_DIR=/mnt/foo
df_result=$(timeout 10 df "$MNT_DIR")
[[ $df_result =~ $MNT_DIR ]] 
if [ "$BASH_REMATCH" = "$MNT_DIR" ]
then
    echo "It is obtainable."
else
    echo "It isn't obtainable."
fi

One other means of Checking Quantity Mounts

mount 
    | minimize -f 3 -d ' ' 
    | grep -q /mnt/foo 
  && echo "mounted" || echo "not mounted"

Leave a Reply