Skip to content
Advertisement

Shell Mount and check directionairy existence

Just looking for some help with my mounting shell script, wondering if anyone could advice me on how to make it check for the directory at the mount point exists and is empty, or is created by the script if it does not exist

JavaScript

Advertisement

Answer

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:

JavaScript

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

JavaScript

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

JavaScript

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

JavaScript

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

JavaScript

In full: (note bash -eu)

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement