LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How to test is a drive is mounted in a shell script? (https://www.linuxquestions.org/questions/linux-general-1/how-to-test-is-a-drive-is-mounted-in-a-shell-script-392668/)

agtlewis 12-14-2005 04:13 AM

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?

cyberjun 12-14-2005 04:29 AM

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

trickykid 12-14-2005 06:17 AM

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

homey 12-14-2005 08:30 AM

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


agtlewis 12-14-2005 09:47 AM

You guys are great:) Thanks a lot for teaching me how to do that!

trickykid 12-14-2005 02:49 PM

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.

agtlewis 12-14-2005 08:03 PM

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.

quequotion 03-18-2009 09:58 AM

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)


All times are GMT -5. The time now is 06:07 PM.