LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   conditional to tell if a drive is mounted using (https://www.linuxquestions.org/questions/linux-newbie-8/conditional-to-tell-if-a-drive-is-mounted-using-4175568126/)

iFunction 01-28-2016 04:00 AM

conditional to tell if a drive is mounted using
 
Hi there,
How can you tell if a drive is mounted. I have a hard drive called Secure_HDD and I am trying to see if is mounted or not.

I found this script to try, but it just comes as Not Mounted when it is clearly mounted and I can write to it;
Code:

#!/bin/bash

if sudo mount | grep media/pi/Secure_HDD/ > /dev/null; then
    echo "Mounted"
else
    echo "Not Mounted"
fi

Ultimately, I have never once successfully got a conditional to work in bash, I can do it in C, C++, Python and VBA, but not bash. For starters just getting this to work will be a ground breaking move. What am I doing wrong please?

grail 01-28-2016 04:37 AM

My first question would be, once mounted, have you confirmed that the grep works?
Code:

sudo mount | grep media/pi/Secure_HDD/
So does the above return anything when not in the bash script and the drive is mounted?

iFunction 01-28-2016 04:46 AM

Hi,

Ok, I have solved my issue of not getting a conditional to work by using:
Code:

#!/bin/bash

if [ -w /media/pi/Secure_HDD/test_file.txt ]; then
    echo "Mounted"
else
    echo "Not Mounted"
fi

However, as I rarely see such a simple solution a this, could someone briefly explain why this might possibly be a poor solution please?

Kind regards
iFunc

grail 01-28-2016 05:47 AM

Are you not able to show the output from the grep command?

Why did you pick -w option, does this file need to be writable by you? Will it always exist? What if it exists but is not writable by you?

pan64 01-28-2016 05:55 AM

not to speak about that file may exist on the root filesystem too, therefore not really safe....
mount will work without sudo.
what about grep -F (or fgrep?)

jmgibson1981 01-28-2016 06:49 PM

Use mountpoint

Code:

if mountpoint -q $yourmountpoint ; then
  echo "mounted"
else
  echo "not mounted"
fi


Habitual 01-28-2016 09:07 PM

n/m.


All times are GMT -5. The time now is 02:15 AM.