LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-28-2016, 05:00 AM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Rep: Reputation: Disabled
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?
 
Old 01-28-2016, 05:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
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?
 
Old 01-28-2016, 05:46 AM   #3
iFunction
Member
 
Registered: Nov 2015
Posts: 248

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-28-2016, 06:47 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,769

Rep: Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052Reputation: 3052
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?
 
Old 01-28-2016, 06:55 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,587

Rep: Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347Reputation: 4347
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?)
 
Old 01-28-2016, 07:49 PM   #6
jmgibson1981
Member
 
Registered: Jun 2015
Location: Tucson, AZ USA
Distribution: Debian
Posts: 412

Rep: Reputation: Disabled
Use mountpoint

Code:
if mountpoint -q $yourmountpoint ; then
  echo "mounted"
else
  echo "not mounted"
fi
 
Old 01-28-2016, 10:07 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Yawnstown, Ohio
Distribution: Mojave
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
n/m.

Last edited by Habitual; 01-28-2016 at 10:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] second hard drive showing first drive / when mounted newbeliever Linux - Newbie 4 01-11-2015 08:11 PM
[SOLVED] MO drive can not be mounted nagib Linux - Hardware 12 11-01-2014 07:10 AM
Pen drive not mounted ...."Unable to mount, No media in drive" subashk80 Linux - Newbie 6 08-17-2009 02:38 AM
Can't keep CD drive mounted Allen Higgins Slackware - Installation 2 02-12-2008 04:21 PM
drive mounted noobtesting Mandriva 3 10-05-2004 08:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:53 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration