Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
Well, the mount command with no arguements gives you evertying that is mounted. So, to see--eg--if /dev/hda3 is mounted, you would do something like:
mount|grep /dev/hda3
and then maybe test the result code
Don't forget /etc/mtab, which shows mounted systems and devices. You could cat /etc/mtab | grep some device, or compare fstab to mtab, and mount any device named in fstab which does not also show in mtab.
You could check, or you could skip the check and just blindly call mount. If it's not mounted, it will mount. If it's already mounted, the command will error out and you can just ignore the error.
The above isn't 100% accurate, but it holds for most filesystem types. A notable exception is an smbfs mount. It will just keep mounting and mounting and mounting without erroring out.
Here's an example for an ntfs mount (a typical scenerio - similar for ext3, etc.):
Code:
# grep familyroom/c /etc/fstab
/dev/hda2 /mnt/familyroom/c ntfs ro,noauto,umask=0222 0 0
# mount | grep familyroom
# mount /mnt/familyroom/c
# mount | grep familyroom
/dev/hda2 on /mnt/familyroom/c type ntfs (ro,umask=0222)
# mount /mnt/familyroom/c
mount: /dev/hda2 already mounted or /mnt/familyroom/c busy
mount: according to mtab, /dev/hda2 is already mounted on /mnt/familyroom/c
# umount /mnt/familyroom/c
# umount /mnt/familyroom/c
umount: /mnt/familyroom/c: not mounted
#
But look what happens for an smbfs mount:
Code:
# grep livingroom /etc/fstab
//192.168.0.53/c /mnt/livingroom/c smbfs noauto,umask=0000,credentials=/etc/cred 0 0
# mount | grep livingroom
# mount /mnt/livingroom/c
# mount | grep livingroom
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
# mount /mnt/livingroom/c
# mount | grep livingroom
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
# mount /mnt/livingroom/c
# mount | grep livingroom
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
//192.168.0.53/c on /mnt/livingroom/c type smbfs (rw)
# umount /mnt/livingroom/c
# umount /mnt/livingroom/c
# umount /mnt/livingroom/c
# umount /mnt/livingroom/c
umount: /mnt/livingroom/c: not mounted
#
Thanks for all of your suggestions. I had been using the script without the error checking but i've grown tired of the error message being displayed. I understand how grep could let me see if it is mounted but I don't know (if it's even possible) how I can translate that into a true/false condition.
Check grep's error status for the translation into fail/success.
grep exits with 0 if it finds some matching lines, 1 if it doesn't or 2 if there was an error.
So, you could do something like (Bash):
cat /etc/mtab | grep some_device
#the exit code of a pipeline is the same as the exit code of the last command in the pipe, except if there's a
#broken pipe
#So, $? will surely contain grep's exit code.
#Store grep's return code in a variable, so that it doesn't get lost ($? is overwritten many times)
grep_code=$?;
if (( $grep_code == 0 )); then
#grep has found matching entries in /etc/mtab, so device is probably mounted
fi;
if (( $grep_code == 1 )); then
#grep didn't find any matches, so device is not mounted
fi;
And likewise for == 2.
A switch/case trick or other control statements may be cleaner code than a bunch of if's, but you can figure
that out from "man your_shell".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.