Check If Directory Mounted with Bash

Check if directory mounted with bash

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.

How to test if a given path is a mount point

I discover that on my Fedora 7 there is a mountpoint command.

From man mountpoint:

NAME
mountpoint - see if a directory is a mountpoint

SYNOPSIS
/bin/mountpoint [-q] [-d] /path/to/directory
/bin/mountpoint -x /dev/device

Apparently it come with the sysvinit package, I don't know if this command is available on other systems.

[root@myhost~]# rpm -qf $(which mountpoint)
sysvinit-2.86-17

How to check if filepath is mounted in OS X using bash?

While @hd1's answer gives you whether the file exists, it does not necessary mean that the directory is mounted or not. It is possible that the file happen to exist if you use this script for different machines or use different mount points. I would suggest this

LOCALMOUNTPOINT="/folder/share"

if mount | grep "on $LOCALMOUNTPOINT" > /dev/null; then
echo "mounted"
else
echo "not mounted"
fi

Note that I include "on" in grep statement based on what mount command outputs in my machine. You said you use MacOS so it should work, but depending on what mount command outputs, you may need to modify the code above.

How to find if a directory is a mount in ShellScript

The key is in saying:

if [ mountpoint -q "$new_dir" ]   # WRONG

You don't have to use test [ because mountpoint -q "$new_dir" already returns a boolean. So this is all you need:

if mountpoint -q "$new_dir"

From man point:

Mountpoint checks if the directory is a mountpoint.

EXIT STATUS

Zero if the directory is a mountpoint, non-zero if not.

In fact you can even say:

mountpoint -q "$new_dir" && echo "mounted" || echo "not mounted"

If you really want to use [ ], then you need to execute the command inside and take into consideration the exit status:

if [ ! $(mountpoint -q "/home") ]; then
echo "directory is mounted"
else
echo "directory is not mounted"
fi

Note I negate the result with [ ! expression ].


Why were you getting a binary operator expected error?

Because you were writing an expression in test that is not supported.

man test explains the syntax it accepts:

[ EXPRESSION ]
[ ]
[ OPTION

And it also mentions that it exits with the status determined by EXPRESSION.

When you say mountpoint within [, bash assumes that it is handling an expression. Given the list of possible conditions (6.4 Bash Conditional Expressions), the fact that the first element is not an operator makes bash want to see a binary operator after it, such as =, =~... Since it sees -q, it fails and mentions -q as the problem.

Bash Detect Mounted Folder as Directory

You should rewrite your if condition to something like that:

if [ ! -e "$DIR" ]; then
echo "Directory does not exist!"
elif [ ! -d "$DIR" ]; then
echo "Not a Directory"
else
echo "Path is okay"
fi

For details see man test

Bash Script - Check mounted devices

Solved! Part of the problem was using mount | grep /media/backup > /dev/null as there's an issue with similar names, so /media/backup2 being mounted meant it was counting both devices as mounted.

Using grep -q /media/backup2\ /proc/mounts works in a similar way, or at least produces the same outcome, just without the naming issue.

I've also changed the way the if statements work, it now checks for SD card, if that fails, then it aborts. It then checks for which devices are mounted with a fairly simple flow.

Here's the code:

GNU nano 2.2.6 File: backupSD2.sh Modified

#!/bin/bash

#Check for SD

if ! grep -q /media/card\ /proc/mounts
then
echo "ERROR: NO SD CARD"
exit 0
else
echo "SD OKAY..."
fi

#Check for Backup Devices

if (( grep -q /media/backup\ /proc/mounts ) && ( grep -q /media/backup2\ /proc/mounts ))
then
echo "Both Backups mounted"

elif grep -q /media/backup\ /proc/mounts
then
echo "CAUTION: Backup_2 missing, Backup_1 OKAY"

elif grep -q /media/backup2\ /proc/mounts
then
echo "CAUTION: Backup_1 missing, Backup_2 OKAY"
else
echo "ERROR: NO BACKUP DEVICES"
exit 0
fi

After the 'echo' commands is where I'll be putting the rsync commands.
The final exit 0 is probably unnecessary, but I don't think it's doing any harm and reassures that it's not going to try any more commands.

Shell script to know whether a filesystem is already mounted

You can check the type of the filesystem.


$ stat -f -c '%T' /
xfs
$ stat -f -c '%T' /dev/shm
tmpfs

You could also check whether a directory is a mountpoint by comparing its device with its parent's.


$ stat -c '%D' /
901
$ stat -c '%D' /home
fe01
$ stat -c '%D' /home/$USER
fe01

Shell Mount and check directionairy existence

Your use of grep will return any mountpoint that contains the string /myfilesystem in... e.g: both of these:

  • /myfilesystem
  • /home/james/myfilesystem

Prefer to use something more prescriptive like the following:

mountpoint -q "${MOUNTPOINT}"

You can use [ to test if a path is a directory:

if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
else
echo "Mountpoint doesn't exist..."
fi
fi

mkdir -p will create all parent directories, as necessary:

mkdir -p "${MOUNTPOINT}"

Finally, test if a directory is empty by exploiting bash's variable expansion:

[ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]

It's also a good idea to run scripts with some level of 'safety'. See the set built-in command: https://linux.die.net/man/1/bash

-e      Exit immediately if a pipeline (which may consist of a single simple command), a
list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero
status.
-u Treat unset variables and parameters other than the special parameters "@" and "*"
as an error when performing parameter expansion.

In full: (note bash -eu)

#!/bin/bash -eu

MOUNTPOINT="/myfilesystem"

if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
exit 1
fi
mkdir -p "${MOUNTPOINT}"
fi

if [ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]; then
echo "Mountpoint is not empty!"
exit 1
fi

if mountpoint -q "${MOUNTPOINT}"; then
echo "Already mounted..."
exit 0
fi

mount "${MOUNTPOINT}"
RET=$?
if [ ${RET} -ne 0 ]; then
echo "Mount failed... ${RET}"
exit 1
fi

echo "Mounted successfully!"
exit 0


Related Topics



Leave a reply



Submit