How to test is a drive is mounted in a shell script?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Why not make it so it takes command line arguments to specify the drive..
Code:
#!/bin/bash
cat /etc/mtab | grep /dev/$1 >/dev/null
if [ "$?" -eq "0" ]; then
echo /dev/$1 is mounted.
else
echo /dev/$1 is not mounted at this time.
fi
Save your script like something like chkmount, cd into the directory it resides in and then you can do something like this to specify the drive you want to check:
I think I'd rather check the mount command to make sure it hasn't been unmounted somewhere along the line.
Code:
#!/bin/bash
mount | grep /dev/hdb1 >/dev/null
if [ "$?" -eq "0" ]; then
echo /dev/hdb1 is mounted.
else
echo /dev/hdb1 is not mounted at this time.
fi
So that means you still have to edit the script with whatever drive you want to check each and every time, why not use my suggestion? Also, using mtab is safe as it's updated every single time you mount or umount a drive in Linux, so using /etc/mtab and or the mount command makes no difference really.
Either way is fine for me, I thing it's a question of what the script is going to be used for. In my situation I am testing 4 static drives so it doesn't need to take any arguments, but since I am running the same test 4 times for different drives it would probably be better to turn it into a function which would take an argument.
What about writing this shell script to check the mount point rather than the mount device? I imagine it would only take a small change, but I am not certain how to go about doing it.
Theoretical model:
if /mount/media == has_something_mounted {
echo "something is mounted";
else;
echo "nothing is mounted";
};
(my plan is to use this for a script to mount and quickly swap iso images)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.