LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-21-2004, 08:49 PM   #1
fortezza
Member
 
Registered: Mar 2003
Location: Colorado
Distribution: Fedora Core 4
Posts: 297

Rep: Reputation: 30
Bash-Check If FS is Already Mounted On Directory


I am wondering if I can use if...fi to test if something is already mounted on a particular directory, so that my script can avoid mounting the multiple Samba shares on a single directory. Currently my script is mounting the same shares multiple times, and that can't be good.

Thanks in advance,
 
Old 01-22-2004, 12:32 AM   #2
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Not sure if this is exactly what you wanted. This script assumes that you pass the directory name to be checked as the first parameter.
Code:
#!/bin/sh

for i in `cat /proc/mounts | cut -d' ' -f2`; do
   if [ "x$1" = "x$i" ]; then
      echo "dude, this directory is already a mount point"
      exit
   fi

done

echo "mount away"
 
Old 01-23-2004, 08:30 AM   #3
fortezza
Member
 
Registered: Mar 2003
Location: Colorado
Distribution: Fedora Core 4
Posts: 297

Original Poster
Rep: Reputation: 30
Clarification

Look at my post more carefully.
>I am wondering if I can use if...fi to test if something is already mounted on a >particular directory

The purpose is not to double mount, triple mount, or more by checking to see if a SambaFS share is already mounted on the directory or not before using the mount command.

What happened is that I made a fstab entry that was user mountable ( noauto ) and then put the mount command in the .bashrc file. However, with this setup the .bashrc file executes everytime a open up a terminal window, and almost always have 3 of these opened at a time. So, with 3 terminal windows open, a mount command will show the duplicate mounts for the directory/mount pairs that are affected by the bashrc script.

So, in short, I want to make sure the script only mounts something on the folder once, and afterwards if will check if the share is already mounted and not execute a mount command if it sees there is already something mounted on the directory.
 
Old 01-23-2004, 10:50 AM   #4
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
That is exactly what my script does. Here is an example of my mount points and how the script executes:
Code:
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda1              3903636   1723728   2179908  45% /
/dev/vg1/opt           2457520   1978524    478996  81% /opt
/dev/vg1/usr           5119836   3157940   1961896  62% /usr
/dev/vg1/home          3145628   1016848   2128780  33% /home
/dev/hdf1             60047208  37018108  23029100  62% /share
Code:
$ checkmount.sh /usr
dude, this directory is already a mount point

$ checkmount.sh /share
dude, this directory is already a mount point

$ checkmount.sh /var
mount away
You can see the directories /usr and /share already have filesystems mounted on them. The script reports that it is already a mount point. The directory /var is on the / filesystem and does not have a filesystem mounted. The script reports 'mount away', so nothing is already mounted on /var.
 
Old 07-01-2009, 11:42 AM   #5
soghad
LQ Newbie
 
Registered: Jul 2009
Posts: 1

Rep: Reputation: 1
Another solution

Here's another solution where the results of cat are piped through grep and the return value is checked.

Code:
cat /proc/mounts | grep /mnt/mymountpoint > /dev/null
if [ $? -eq 0 ] ; then
  echo The path /mnt/mymountpoint is mounted
fi
 
Old 08-09-2010, 10:35 PM   #6
noahspurrier
LQ Newbie
 
Registered: Dec 2008
Location: San Francisco, CA
Distribution: Ubuntu
Posts: 1

Rep: Reputation: 0
Exclamation

Quote:
Originally Posted by soghad View Post
Here's another solution where the results of cat are piped through grep and the return value is checked.

Code:
cat /proc/mounts | grep /mnt/mymountpoint > /dev/null
if [ $? -eq 0 ] ; then
  echo The path /mnt/mymountpoint is mounted
fi
Alert! Useless Use Of Cat!

Also you should specify whitespace or an anchor around the pattern you are looking for; otherwise, longer strings will match your shorter pattern. For example, what if you wanted to know if /mnt/USB was mounted and you happen to have /mnt/USB2 mounted? Your pattern would match /mnt/USB2 even if /mnt/USB was not mounted.

POSIX grep will support the '-q' option, so you don't need >/dev/null if you don't care about seeing the match.

Finally, you can combine the 'if' statement and 'grep' into a single expression. You don't need to check the exit code with $? explicitly -- that's what 'if' does! The square brackets return an exit code which is what 'if' is really looking at. The '[' is the name of an actual program. Type 'which [' at the command-line. You should get "/usr/bin/[".

Here is something a little cleaner:
Code:
if grep -q "[[:space:]]/mnt/mymountpoint[[:space:]]" /proc/mounts; then
    echo The path /mnt/mymountpoint is mounted
fi
And as a final, final note, if someone were really evil they could created a mount point with a space in the name. That would seem to break the pattern I gave, but luckily Linux escapes funny characters using their octal value. So if you created and mounted a mount point like this: mkdir "/mnt/mymountpoint too" it would show up in /proc/mounts as /mnt/mymountpoint\040too and so, would not match the pattern "[[:space:]]/mnt/mymountpoint[[:space:]]".

--
Noah
 
Old 08-09-2010, 10:51 PM   #7
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by fortezza View Post
I am wondering if I can use if...fi to test if something is already mounted on a particular directory, so that my script can avoid mounting the multiple Samba shares on a single directory. Currently my script is mounting the same shares multiple times, and that can't be good.

Thanks in advance,
Hi,

you could have a look at
Code:
man mountpoint
This will tell you if a directory is already a mountpoint. The exit status will be zero if /your/mount/dir is already a mountpoint, otherwise non-zero.
Hope this helps.
 
1 members found this post helpful.
Old 08-10-2010, 06:31 AM   #8
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
I agree with crts, use mountpoint. Eg
Code:
mountpoint -q /mnt/usb || mount /mnt/usb
 
1 members found this post helpful.
Old 08-10-2010, 03:57 PM   #9
crabboy
Moderator
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
I'm sure it sounds great now, but back in 2004 the mountpoint command did not exist. I'm sure there are thousands of posts that can be improved upon on LQ, but I think I'd be more beneficial to focus your new found skills on more recent posts rather than dig up and improve solutions from 6 years ago.
 
0 members found this post helpful.
Old 08-10-2010, 08:39 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by crabboy View Post
I'm sure it sounds great now, but back in 2004 the mountpoint command did not exist. I'm sure there are thousands of posts that can be improved upon on LQ, but I think I'd be more beneficial to focus your new found skills on more recent posts rather than dig up and improve solutions from 6 years ago.
Hmm, did not see that this was a resurrected post from six years ago. I really wonder why people take the time to search for such ancient posts and think it is a good idea to give advice on a long dead thread.
 
Old 10-19-2010, 12:44 PM   #11
wallaceg
LQ Newbie
 
Registered: Sep 2007
Posts: 4

Rep: Reputation: 3
What's wrong with adding good information? I appreciated the post - it was exactly what I was looking for.

Perhaps it could have been framed in a different context, like: "These days, the mountpoint command is available ...". Or since not everyone knows the exact moment a command is born and made available to the world, "Maybe mountpoint(8) was not available it 2004, but ...".

Improving this older post seems perfectly okay to me, especially if people have been (and will continue to) use the only hackish solutions that _used_ to be available when a better one is available now.

Perhaps a more open-minded approach to moderation could be used here.
 
3 members found this post helpful.
Old 01-22-2011, 02:43 PM   #12
cdaaawg
LQ Newbie
 
Registered: Apr 2007
Location: Tacoma, WA
Distribution: Ubuntu 7.10
Posts: 1

Rep: Reputation: 1
agreed!

I agree wholeheartedly with wallaceg's assertion that improving on a "hack" contained in a post no matter the age is a good thing. Indeed, with the nature of search results, it could be deemed a necessity IMNSHO.

I too found the post regarding mountpoint helpful.

Thanks for any constructive input.

Carl
 
1 members found this post helpful.
Old 05-17-2011, 03:30 PM   #13
ukt
LQ Newbie
 
Registered: Aug 2006
Location: UK
Distribution: ubuntu
Posts: 1

Rep: Reputation: 0
Thumbs up Thanks for resurrected thread...

As a 'yet again' Unix/Linux convert with a ~30yr tenuous hit 'n miss Unix affair, I am grateful for seeing the KISS principle used [maybe stepwise refinement sounds better?].
Quote:
I agree with crts, use mountpoint. Eg
Code:
mountpoint -q /mnt/usb || mount /mnt/usb
The various posts are extremely informative and a joy to read and [for me at least] ponder over with other tabs open researching out the various syntax items.
The above code is beyond me [but not for long:] and I can easily see the relevance of it's simplicity. EG, I perhaps incorrectly see that the boolean cond will dip out/short-circuit IF device is mounted ELSE mount it.

Wow... for me this is pure elegance. I'm still struggling with the overall *nix paradigm but have done nuff programming to at least see the possible meaning of that line of code...

imho, Improving on any code is great. As long as it is relevant and not malicious in nature nor just 'yet another' hack that actually, does not improve what went before. But yet, pondering over any/all code is brilliant way to learn and coupled with various posts comments, learn to be more critical/analytical (in a +ve way)

Peer review and [constructive/informative] commentary is what [I feel] should get the juices flowing and cause one to get the terminal pane up and start playing!

Just what I'm off to do now... Seriously, I'm so totally at the crawling stage with Bash and all the zillions of other exciting facets of such a rich computing environment that is *nix.

Excuse the ramble - but I honestly enjoy threads like this. So much can be learned.

Cheers
 
Old 09-21-2011, 07:38 AM   #14
marzvix
LQ Newbie
 
Registered: Sep 2011
Location: Vitoria
Distribution: Debian, Finnix, Ubuntu
Posts: 1

Rep: Reputation: Disabled
Since Darwin...

Evolution is welcome.

mountpoint get my job done here !

Thx.
 
1 members found this post helpful.
Old 01-07-2012, 01:44 AM   #15
scorpionstingnz
LQ Newbie
 
Registered: Jan 2012
Posts: 1

Rep: Reputation: Disabled
Function

Here's a function I've just put together from the above posts:

Code:
function mountFS() {
    mp=${1}
    sd=${2:-"false"}
    if grep -q "[[:space:]]${mp}[[:space:]]" /proc/mounts; then
        echo "${mp} already mounted"
    else
        if [ ${sd} == "true" ]
         then
            sudo mount ${mp}
        else
            mount ${mp}
        fi
    fi
}
Then you can just call it with:

Code:
mountFS /mnt/whatever
of, if you require su rights and are allowed to sudo without password:

Code:
mountFS /mnt/whatever true
 
  


Reply

Tags
bash


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
Check to see if a drive is mounted joshnya Linux - Newbie 2 11-07-2005 09:56 AM
many hdds mounted on 1 directory kabitak Linux - Networking 2 11-18-2004 05:03 AM
script check to see if device is mounted Warmduvet Programming 14 09-03-2004 07:17 PM
can i have a few partitions mounted in one directory ? baronlynx Linux - Newbie 1 02-27-2004 04:46 AM
Check if it IS mounted sourceman Linux - General 11 02-27-2002 09:31 AM


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