Operating System - HP-UX
1826106 Members
4711 Online
109690 Solutions
New Discussion

checking mount point directory

 
SOLVED
Go to solution
Damian Benavent Pla
Occasional Contributor

checking mount point directory

Is there any operator in POSIX shell to check if a directory is a mount point ?

test command (and shell built-in test command) has got some operators like "-f", "-x", ... etc.
15 REPLIES 15
Andreas Voss
Honored Contributor

Re: checking mount point directory

Hi,

i don't know any test operator for this but here my suggestion:

grep -q " $dir " /etc/mnttab
if [ $? = 0 ]
then
echo "Directory $dir is a mount point"
else
echo "Directory $dir is NOT a mount point"
fi

The variable dir has to be the absolute path.

Regards
MARTINACHE
Respected Contributor

Re: checking mount point directory

Hi,

Supposing that $DIRNAME is the name of your directory.

You can do :

if [ $DIRNAME = `bdf $DIRNAME | tail -1 | awk {'print $6}'` ];
then ...
else ...
fi

Regards,

Patrice
Patrice MARTINACHE
James R. Ferguson
Acclaimed Contributor

Re: checking mount point directory

Hi Damian:

One way is to grep for the mountpoint in question in /etc/fstab:

WHAT=/home
if [ `grep -c $WHAT /etc/fstab` -gt 0 ]
then
echo "$WHAT is a mountpoint"
else
echo "$WHAT isn't a mountpoint"
fi

...JRF...
Dan Hetzel
Honored Contributor

Re: checking mount point directory

Hi Damian,

As usual, many ways lead to Rome...

[ $DIR = $(df $DIR | cut -f1 -d" ") ] && echo mount point

DIR must contain a full pathname

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Carlos Fernandez Riera
Honored Contributor

Re: checking mount point directory

I have read man sh-posix and i dont find any flag for test that return if is mount-point.


To write a simple code i would prefer use of moun command instead of /etc/fstab: mount_point are directory and when it has a file_system mounted is a mount_point

D=/home
MP=`mount | grep -c "^$D "`

echo $MP



unsupported
Sandor Horvath_2
Valued Contributor

Re: checking mount point directory

Hi !

Other way>

/usr/sbin/mount | cut -d" " -f 1 | grep $(pwd)
if [ $? -eq 0 ]
then
echo "$(pwd) is mount point "
fi

regards, Saa
$(pwd) is actual directory You can change it any variable
If no problem, don't fixed it.
MARTINACHE
Respected Contributor

Re: checking mount point directory

Hi,

Some of the answers will not work in this :

You have 2 mountpoins :

/firstmountpoint
/firstmountpoint/adirectory/secondmountpoint

If you check /firstmountpoint/adirectory,
some of the previous will answer OK but its false.

My solution works in this case.

Regards,

Patrice.
Patrice MARTINACHE
James R. Ferguson
Acclaimed Contributor

Re: checking mount point directory

Hi (again):

Patrice makes a very good point. A better evaluation of /etc/fstab should look like this:

WHAT=[[:space:]]/home[[:space:]]
if [ `grep -c $WHAT /etc/fstab` -gt 0 ]
then
echo "$WHAT is a mountpoint"
else
echo "$WHAT isn't a mountpoint"
fi

This puts white-space surrounding the /home token in the example chosen, and correctly queries /etc/fstab for its presence, unambiguously.

I choose to interrogat /etc/fstab rather with the assumption that one is interested in directories which have been defined for mounting as opposed to directories which are already mounted.

Regards!

...JRF...
MARTINACHE
Respected Contributor

Re: checking mount point directory

Hi (again),

James, your script will not work only if you use Service Guard (Mountpoint are in package configuration files)

;-)

Patrice.
Patrice MARTINACHE
James R. Ferguson
Acclaimed Contributor

Re: checking mount point directory

Hi Patrice:

EXCELLENNT POINT!...and I run MC/ServiceGuard too!!!...

One of the things I've done in my cluster's fstab files is to place the pound (#) mark over the device special file name (field-1). This reminds me of the mount point although 'mount' complains mildly during bootup.

With my regards and my thanks for your corrections, Jim. :-))

...JRF...
Carlos Fernandez Riera
Honored Contributor

Re: checking mount point directory

Gooooood .... Discussion.

Maybe you know why i prefer mount command!!

I did not think in S/G neither.

Note my grep contain caret "^" This is begin of line and space " " as separator.

Fine, Patrice.


unsupported
Andy Monks
Honored Contributor
Solution

Re: checking mount point directory

Everyone seems to be overlooking the easist and most reliable way :-

#!/usr/bin/sh
if [ `/usr/bin/ls -ldi . | awk '{ print $1}'` = 2 ]
then
echo This is a mount point
else
echo This is not a mount point
fi

Each filesystem has it's own *root* directory. All root directories have the inode number 2 :-)

Andy
Dan Hetzel
Honored Contributor

Re: checking mount point directory

Hey Andy,

That's by far the most elegant solution !! You deserve at least 20 (or more) for that one.

It's surprising that no one else even thought about that one...

You kicked our a$@es !!
8-(

Good show !

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Carlos Fernandez Riera
Honored Contributor

Re: checking mount point directory

My 10 points to Andy 8-O.

Now, we can go far away:

Find / -inum 2

Jim: Following your Philosofy you must add /SD_CDROM, /cdrom, /tmp_mnt ...

unsupported
Damian Benavent Pla
Occasional Contributor

Re: checking mount point directory

Hi boys and girls.

I thank everybody for your help.

I agree with Dan: Andy Monks suggested the most elegant solution.

I'll try to post other interesting questions...