Check if directory is installed with bash

I use

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

I want to check /foo/bar , albeit with a bash script, and see if it is mounted? If not, then call the mount command above, otherwise do something else. How can i do this?

CentOS is an operating system.

+61
linux bash centos mount
Feb 23 2018-12-23T00:
source share
6 answers

Running mount with no arguments will show you the current settings. From a shell script, you can check the mount point with grep and if-statement:

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

In my example, the if statement checks the grep exit code, which indicates whether there was a match. Since I do not want the output to be displayed when there is a match, I redirect it to /dev/null .

+51
Feb 23 '12 at 23:12
source share

You did not bother to mention O / S.

Ubuntu Linux 11.10 (and possibly the most modern Linux variants) have a mountpoint command.

Here is an example on one of my servers:

 $ mountpoint /oracle /oracle is a mountpoint $ mountpoint /bin /bin is not a mountpoint 

Actually, in your case, you can use the -q option, for example:

 mountpoint -q /foo/bar || mount -o bind /some/directory/here /foo/bar 

Hope this helps.

+129
Feb 23 2018-12-23T00:
source share

Another clean solution:

 $ mount | grep /dev/sdb1 > /dev/null && echo mounted || echo unmounted 

Of course, "echo something" can be replaced with what you need to do for each case.

0
Mar 28 '17 at 13:11
source share

The mountpoint manual states that it:

checks if a given directory or file is specified in the / proc / self / mountinfo file.

The mount manual says that:

Listing mode is only supported for backward compatibility. For a more reliable and customizable output, use findmnt (8), especially in your scripts.

So, the correct command to use is findmnt , which itself is part of the util-linux package and, according to the manual:

able to search in / etc / fstab, / etc / mtab or / proc / self / mountinfo

Therefore, he is looking for more things than mountpoint . It also provides a convenient option:

-M, - mount point path

Explicitly define the file or directory of the mount point. See Also --target.

In conclusion, to check if a directory is installed using bash, you can use:

 if [[ $(findmnt -M "$FOLDER") ]]; then echo "Mounted" else echo "Not mounted" fi 



Example:

 mkdir -p /tmp/foo/{a,b} cd /tmp/foo sudo mount -o bind ab touch a/file ls b/ # should show file rm -fb/file ls a/ # should show nothing [[ $(findmnt -M b) ]] && echo "Mounted" sudo umount b [[ $(findmnt -M b) ]] || echo "Unmounted" 
0
Sep 03 '17 at 16:38 on
source share

My decision:

 is_mount() { path=$(readlink -f $1) grep -q "$path" /proc/mounts } 

Example:

 is_mount /path/to/var/run/mydir/ || mount --bind /var/run/mydir/ /path/to/var/run/mydir/ 

For Mark J. Bobak's answer , mountpoint does not work if mount with the bind parameter is on different file systems.

For Christopher Neilan the answer is , you do not need to redirect grep output to / dev / null, use grep -q instead.

Most importantly, readlink -f $mypath path using readlink -f $mypath :

  • If you check the path, for example /path/to/dir/ , end the backslash, the path in /proc/mounts or mount will be /path/to/dir
  • On most Linux releases, /var/run/ is a symbolic link /run/ , so if you mount a bind for /var/run/mypath and check if it is mounted, it will display as /run/mypath in /proc/mounts .
0
Nov 10 '17 at 2:19
source share

In my .bashrc, I made the following alias:

 alias disk-list="sudo fdisk -l" 
-2
Jun 18 '17 at 1:15
source share



All Articles