What's the best way to check if a volume is mounted in a Bash script?
Solution 1:
Avoid using /etc/mtab
because it may be inconsistent.
Avoid piping mount
because it needn't be that complicated.
Simply:
if grep -qs '/mnt/foo ' /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."
fi
(The space after the /mnt/foo
is to avoid matching e.g. /mnt/foo-bar
.)
Solution 2:
if mountpoint -q /mnt/foo
then
echo "mounted"
else
echo "not mounted"
fi
or
mountpoint -q /mnt/foo && echo "mounted" || echo "not mounted"
Solution 3:
findmnt -rno SOURCE,TARGET "$1"
avoids all the problems in the other answers. It cleanly does the job with just one command.
Other approaches have these downsides:
grep -q
andgrep -s
are an extra unnecessary step and aren't supported everywhere./proc/\*
isn't supported everywhere. (mountpoint
is also based on proc).mountinfo
is based on /proc/..cut -f3 -d' '
messes up spaces in path names- Parsing mount's white space is problematic. It's man page now says:
.. listing mode is maintained for backward compatibility only.
For more robust and customizable output use findmnt(8), especially in your scripts.
Bash functions:
#These functions return exit codes: 0 = found, 1 = not found
isMounted () { findmnt -rno SOURCE,TARGET "$1" >/dev/null;} #path or device
isDevMounted () { findmnt -rno SOURCE "$1" >/dev/null;} #device only
isPathMounted() { findmnt -rno TARGET "$1" >/dev/null;} #path only
#where: -r = --raw, -n = --noheadings, -o = --output
Usage examples:
if isPathMounted "/mnt/foo bar"; #Spaces in path names are ok.
then echo "path is mounted"
else echo "path is not mounted"
fi
if isDevMounted "/dev/sdb4";
then echo "device is mounted"
else echo "device is not mounted"
fi
#Universal:
if isMounted "/mnt/foo bar";
then echo "device is mounted"
else echo "device is not mounted"
fi
if isMounted "/dev/sdb4";
then echo "device is mounted"
else echo "device is not mounted"
fi
Solution 4:
A script like this isn't ever going to be portable. A dirty secret in unix is that only the kernel knows what filesystems are where, and short of things like /proc (not portable) it'll never give you a straight answer.
I typically use df to discover what the mount-point of a subdirectory is, and what filesystem it is in.
For instance (requires posix shell like ash / AT&T ksh / bash / etc)
case $(df $mount)
in
$(df /)) echo $mount is not mounted ;;
*) echo $mount has a non-root filesystem mounted on it ;;
esac
Kinda tells you useful information.
Solution 5:
the following is what i use in one of my rsync backup cron-jobs. it checks to see if /backup is mounted, and tries to mount it if it isn't (it may fail because the drive is in a hot-swap bay and may not even be present in the system)
NOTE: the following only works on linux, because it greps /proc/mounts - a more portable version would run 'mount | grep /backup', as in Matthew's answer..
if ! grep -q /backup /proc/mounts ; then if ! mount /backup ; then echo "failed" exit 1 fi fi echo "suceeded." # do stuff here