LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-23-2008, 03:01 AM   #1
moschi
Member
 
Registered: Mar 2004
Location: boulder, co
Distribution: slack 12, debian 4, ubuntu server 6.10
Posts: 68

Rep: Reputation: 15
bash: test if device is mounted


in a bash script, is there a way to test if a device is mounted using primaries (ie. [ -f /foobar/foo.bar ] etc...) or "test"

basically want to do an if, then, else that varies on whether or not a specific device is mounted. the dev always mounts to the same location set through fstab, so using the mounted location is a possibility too. i just can't seem to find any specifics that differ when mounted or not.

if worse comes to worse, i can just put a file on that device & use
Code:
if [ -f /foo/bar/.randomfile ]; then
echo "blah";
else echo "bloh";
fi
but i'd like to avoid that if it's possible.

thanks in advance.
 
Old 02-23-2008, 03:43 AM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,485

Rep: Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077Reputation: 1077
grep the output from df.
 
Old 02-23-2008, 03:46 AM   #3
shadowsnipes
Senior Member
 
Registered: Sep 2005
Distribution: Slackware
Posts: 1,442

Rep: Reputation: 70
If you know it is always going to be mounted at the same place then you can do something simple like this

Code:
if [ $(mount | grep -c /mnt/MOUNTEDDIR) != 1 ]
then
        /sbin/mount /mnt/MOUNTEDDIR || exit 1
        echo "/mnt/MOUNTEDDIR is now mounted"
else
        echo "/mnt/MOUNTEDDIR already mounted"
fi
The mount command with no args simply lists what is mounted and grep -c counts how many matches it finds. There should only be one match if your device is mounted. You could also specify the device in cases where the mount point isn't necessarily known.
 
Old 02-23-2008, 04:19 AM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Same thing as syg00 and shadowsnipes but, FWIW, I'd write it
Code:
if mount | grep -q /mnt/MOUNTEDDIR; then...
 
Old 02-23-2008, 04:32 AM   #5
moschi
Member
 
Registered: Mar 2004
Location: boulder, co
Distribution: slack 12, debian 4, ubuntu server 6.10
Posts: 68

Original Poster
Rep: Reputation: 15
great.
thanks for the quick replies.
i totally spaced out that mount & df give a list of what's mounted. oops.
 
Old 04-12-2009, 04:23 PM   #6
daggilli
LQ Newbie
 
Registered: Apr 2009
Location: Paradise
Distribution: Ubuntu Dapper+
Posts: 1

Rep: Reputation: 0
Many Linux distros have the mountpoint(1) command which can explicitly be used to test if a directory is a mountpoint.

Code:
#!/bin/bash
mountpoint -q $1
if [ $? == 0 ]
then
echo "$1 is a mountpoint"
else
echo "$1 is not a mountpoint"
fi
The -q option to mountpoint runs it in silent mode so only the exit status is returned.

There is no equivalent in BSD as far as I can tell. Note that if using the mount + grep -c method, you may need to use a regular expression to test the mountpoint. I have a an old drive whose mountpoints that were previously mounted at the root level are now mounted one level down i.e. the old disk has mount points like /olddisk/usr, /olddisk/home etc. The current /usr partition is also a mountpoint. A simple grep -c on /usr will therefore return 2.
 
Old 06-29-2009, 03:19 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Good note. <pedantic>Though, I'd still write it as

Code:
#!/bin/bash

if mountpoint -q $1; then
    echo "$1 is a mountpoint"
else
    echo "$1 is not a mountpoint"
fi
It's a little easier to type, a little cleaner to read and, if you end up inserting lines in a complex script, you don't have to worry about losing track of the value of $?, which is volatile.

Also, technically, when using the old style test, it should be a single '=' (for string comparison) or '-eq' for integer comparison rather than the double '==' (ash, for instance, will throw an 'unexpected operator' error). And, if using bash as our shebang declares, it should use the new style test, anyway. Just making the command's exit status the conditional avoids the historical baggage of the tests.</pedantic>

Still, the essence of it is that mountpoint, if available, would definitely have been the command to use.
 
Old 03-02-2010, 11:30 AM   #8
lewkor
LQ Newbie
 
Registered: Oct 2006
Posts: 1

Rep: Reputation: 0
try the proc file system

I know that this thread is not current, but I had the same query as to how to do this from a program. What I found is that /proc/self/mounts has the same info as what mount gives and it also has info on mounts that have not been made through the mount program - ie. mounts that have been made through the mount system call.

Explicitly, what you would do is the following command (or equivalent):

grep mountpoint /proc/self/mounts
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test is a drive is mounted in a shell script? agtlewis Linux - General 7 03-18-2009 10:58 AM
USB device not mounted rob2006 Mandriva 9 06-12-2007 09:59 AM
testing if device is already mounted kushalkoolwal Programming 7 09-30-2005 12:40 PM
swap device not getting mounted jacobselvin Linux - Newbie 9 07-20-2005 10:48 AM
Mounted Device names Rodzilla Linux - Newbie 7 09-25-2003 11:39 AM


All times are GMT -5. The time now is 09:25 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration