• Javascript
  • Python
  • Go

Check Mounted Status of Filesystem using Shell Script

Title: Check Mounted Status of Filesystem using Shell Script The filesystem is an integral part of any operating system, responsible for man...

Title: Check Mounted Status of Filesystem using Shell Script

The filesystem is an integral part of any operating system, responsible for managing and organizing the storage of data. In Linux, the filesystem is represented by a hierarchical structure, with the root directory at the top and various subdirectories and files nested within it. As a system administrator, it is crucial to keep track of the mounted status of the filesystem to ensure smooth functioning and prevent any potential issues. In this article, we will explore how to use a shell script to check the mounted status of the filesystem in Linux.

Firstly, let us understand what mounting means in the context of the filesystem. Mounting is the process of integrating a storage device or partition into the existing filesystem hierarchy. This allows the operating system to access and use the storage device as a part of the overall filesystem. In Linux, the mount command is used to mount a device or partition, and the umount command is used to unmount it.

Now, let's move on to creating a shell script to check the mounted status of the filesystem. We will be using the df command, which displays the disk space usage of all mounted filesystems, to gather the required information. Open your favorite text editor and create a new file named "check_mount.sh". Begin by declaring the shebang line, #!/bin/bash, to indicate that this is a bash script.

Next, we need to get the output of the df command and store it in a variable. We can do this by using command substitution, where the output of a command is substituted in place of a command as a string. The syntax for command substitution is $(command). So, in our case, we will use the df command within the $() to get the output and save it in a variable named "df_output".

Now, we need to process this output to check if any filesystems are not mounted. We can do this by using the grep command, which searches for a specified pattern in the given input. We will use the "-v" option with grep to invert the matching, i.e., it will print all lines that do not match the given pattern. In this case, the pattern we are looking for is "Filesystem", as it is a header line in the output of the df command and does not represent a mounted filesystem. So, we will use the following command to get only the lines that represent mounted filesystems:

grep -v "Filesystem" <<< "$df_output"

Next, we need to check if the output of this command is empty or not. If it is empty, it means that all filesystems are mounted, and we can print a message saying so. Otherwise, we can print the list of filesystems that are not mounted. To check if the output is empty, we will use the "-z" option with the test command, which checks if a string is empty or not. The syntax for this is [ -z string ]. So, we will use the following if-else statement:

if [ -z "$mounted_fs" ]; then

echo "All filesystems are mounted."

else

echo "The following filesystems are not mounted:"

echo "$mounted_fs"

fi

Finally, we can save and close the file. Before we can execute this script, we need to make it executable using the chmod command:

chmod +x check_mount.sh

Now, we can run the script using the ./check_mount.sh command. If all filesystems are mounted, we will see the message "All filesystems are mounted." If there are any filesystems that are not mounted, we will see the list of those filesystems.

In conclusion, using a simple shell script, we can easily check the mounted status of the filesystem in Linux. This helps us to ensure that all storage devices and partitions are properly mounted and ready for use. It also allows us to quickly identify and troubleshoot any issues related to the filesystem. As a system administrator, it is essential to regularly check the mounted status of the filesystem to maintain a stable and reliable system.

Related Articles

Align Text to the Right - Bash

In the world of coding, there are many different languages and tools that developers use to create and manipulate code. One of these tools i...

Reading Directory Contents in Perl

Perl is a powerful and versatile programming language that is used for a wide range of tasks, from system administration to web development....