LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 12-14-2005, 04:13 AM   #1
agtlewis
Member
 
Registered: Oct 2005
Distribution: Fedora 4
Posts: 40

Rep: Reputation: 15
How to test is a drive is mounted in a shell script?


I am writing a shell script and have been searching for a reliable method of testing whether or not a drive is mounted.

Any ideas?

Last edited by agtlewis; 12-14-2005 at 09:52 AM.
 
Old 12-14-2005, 04:29 AM   #2
cyberjun
Member
 
Registered: Mar 2005
Distribution: Gentoo kernel 2.6.14
Posts: 96

Rep: Reputation: 15
hi,
Code:
   cat /etc/mtab | grep /dev/hda6 >/dev/null
   if [ "$?" -eq "0" ]; then
        echo mounted
   else
        echo not mounted
   fi
am I missing something?

cheers,
--cyberjun
 
Old 12-14-2005, 06:17 AM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by cyberjun
am I missing something?
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:

Code:
./chkmount hda3
 
Old 12-14-2005, 08:30 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
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
 
Old 12-14-2005, 09:47 AM   #5
agtlewis
Member
 
Registered: Oct 2005
Distribution: Fedora 4
Posts: 40

Original Poster
Rep: Reputation: 15
You guys are great Thanks a lot for teaching me how to do that!
 
Old 12-14-2005, 02:49 PM   #6
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by homey
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.
 
Old 12-14-2005, 08:03 PM   #7
agtlewis
Member
 
Registered: Oct 2005
Distribution: Fedora 4
Posts: 40

Original Poster
Rep: Reputation: 15
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.
 
Old 03-18-2009, 09:58 AM   #8
quequotion
LQ Newbie
 
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
reverse check?

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)
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
execute shell script when USB drive plugged in OneSeventeen Linux - Hardware 2 10-28-2005 10:27 AM
how to test if user is root or not in a shell script? dr_zayus69 Programming 3 08-17-2005 03:00 AM
Shell script to test my shell alltime Programming 5 04-22-2005 07:24 PM
Req. Shell script: Network performance test altu Solaris / OpenSolaris 6 08-08-2004 11:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:59 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