LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 02-23-2006, 08:23 PM   #1
seagoj
LQ Newbie
 
Registered: Apr 2004
Posts: 4

Rep: Reputation: 0
Check for mounted device from shell


I'm looking for a way to check for a mounted device from a shell script, such as:

if ( mounted )
...
else
mount

Very possibly a dumb question, but I would appreciate any help you can give me.

Thanks
 
Old 02-23-2006, 08:59 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 02-23-2006, 09:34 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian stable
Posts: 5,908

Rep: Reputation: 356Reputation: 356Reputation: 356Reputation: 356
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.
 
Old 02-23-2006, 10:13 PM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Quote:
Originally Posted by seagoj
if ( mounted )
...
else
mount
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
#
 
Old 02-23-2006, 11:24 PM   #5
seagoj
LQ Newbie
 
Registered: Apr 2004
Posts: 4

Original Poster
Rep: Reputation: 0
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.

Thanks for the help.
 
Old 02-24-2006, 09:54 AM   #6
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,467

Rep: Reputation: Disabled
To stop errors displaying put 2>/dev/null at end of mount command lines
 
Old 02-24-2006, 10:20 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash-Check If FS is Already Mounted On Directory fortezza Programming 15 04-27-2013 07:06 PM
Check to see if a drive is mounted joshnya Linux - Newbie 2 11-07-2005 08:56 AM
script check to see if device is mounted Warmduvet Programming 14 09-03-2004 06:17 PM
Mounted Device names Rodzilla Linux - Newbie 7 09-25-2003 10:39 AM
Check if it IS mounted sourceman Linux - General 11 02-27-2002 08:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration