LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash-Check If FS is Already Mounted On Directory (http://www.linuxquestions.org/questions/programming-9/bash-check-if-fs-is-already-mounted-on-directory-137282/)

fortezza 01-21-2004 07:49 PM

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,

crabboy 01-21-2004 11:32 PM

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"


fortezza 01-23-2004 07:30 AM

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.

crabboy 01-23-2004 09:50 AM

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.

soghad 07-01-2009 10:42 AM

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


noahspurrier 08-09-2010 09:35 PM

Quote:

Originally Posted by soghad (Post 3593097)
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

crts 08-09-2010 09:51 PM

Quote:

Originally Posted by fortezza (Post 714217)
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.

Andrew Benton 08-10-2010 05:31 AM

I agree with crts, use mountpoint. Eg
Code:

mountpoint -q /mnt/usb || mount /mnt/usb

crabboy 08-10-2010 02:57 PM

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.

crts 08-10-2010 07:39 PM

Quote:

Originally Posted by crabboy (Post 4062267)
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.

wallaceg 10-19-2010 11:44 AM

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.

cdaaawg 01-22-2011 01:43 PM

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

ukt 05-17-2011 02:30 PM

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

marzvix 09-21-2011 06:38 AM

Since Darwin...
 
Evolution is welcome.

mountpoint get my job done here !

Thx.

scorpionstingnz 01-07-2012 12:44 AM

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


All times are GMT -5. The time now is 11:36 PM.