LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-30-2007, 04:01 PM   #1
Cathinfo
LQ Newbie
 
Registered: Dec 2006
Posts: 19

Rep: Reputation: 0
Can Bash determine if /mnt/place is mounted on?


Say you have your typical

/mnt/point

and you want to know if something has been mounted on that point.

How can you do that in a Bash script?

Sometimes you want to know if the directory is "just a directory" or if it refers to an external USB drive, hard drive, etc.

Thanks,

Matthew
 
Old 07-30-2007, 04:09 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
"grep -q /mnt/point /proc/mounts && echo mounted"?
 
Old 07-30-2007, 04:14 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The mount command without any argument list all the mounted devices and their mount points, along with other information. You can grep the output from mount to look for a line containing that particular mount point: if it does not exist, nothing is mounted on that location.

EDIT: sorry, posted before viewing the reply from unSpawn. Nice solution!

Last edited by colucix; 07-30-2007 at 04:17 PM.
 
Old 07-30-2007, 04:18 PM   #4
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Or, /etc/mtab contains information about mounted filesystems, you can grep that too.
 
Old 07-30-2007, 04:33 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by colucix
The mount command without any argument list all the mounted devices and their mount points, along with other information. You can grep the output from mount to look for a line containing that particular mount point: if it does not exist, nothing is mounted on that location.

EDIT: sorry, posted before viewing the reply from unSpawn. Nice solution!
In my experience, /proc/mounts is more useful than the output of mount since mount will just read /etc/mtab, but if / is r/o then new mounts won't be written to it. Also, mount can be called so it doesn't write to /etc/mtab and there is a mount system call which allows programs other than the command proper to mount file systems, which aren't required to update /etc/mtab. Yes, it certainly is a PITA to maintain a system with a r/o root partition, which is why I've stopped doing it, but the point is the kernel itself is a better source of what's mounted.
ta0kira

PS The mount command would be the portable way to check, though. Is there a standard output format across Unixes?

Last edited by ta0kira; 07-30-2007 at 04:34 PM.
 
Old 07-30-2007, 04:53 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by ta0kira
...but the point is the kernel itself is a better source of what's mounted.

PS The mount command would be the portable way to check, though. Is there a standard output format across Unixes?
Thank you for the further explanation! Indeed, my use of mount command comes from Unix (SGI and SunOS) where /proc/mount does not exist. By the way, I'm afraid there is not a standard format. Here is an example from SunOS
Code:
/ on /dev/md/dsk/d0 read/write/setuid/intr/largefiles/logging/onerror=panic/dev=1540000 on Tue Jun 26 16:38:06 2007
/proc on /proc read/write/setuid/dev=4a00000 on Tue Jun 26 16:38:03 2007
 
Old 07-30-2007, 07:58 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Another way:
Code:
if [ "`stat -c%d /`" -ne "`stat -c%d /mnt/cdrom`" ]; then
  echo different file systems
else
  echo same file system
fi
That's GNU stat, BTW. I haven't tried on BSD, but I don't think "normal" stat supports -c.
ta0kira
 
Old 07-31-2007, 06:37 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
On a side note, this problem is an excellent example of why user-mountable devices are by default noexec, nosuid, etc., and why external devices should be mounted with those options, anyway.

What is it you are attempting to prevent/detect? Is this a security or archiving issue, by any chance?
ta0kira
 
Old 07-31-2007, 07:05 PM   #9
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by ta0kira
Another way:
Code:
if [ "`stat -c%d /`" -ne "`stat -c%d /mnt/cdrom`" ]; then
  echo different file systems
else
  echo same file system
fi
That's GNU stat, BTW. I haven't tried on BSD, but I don't think "normal" stat supports -c.
ta0kira

There is no normal stat command. There are at least two different versions of the command, and their options (and default output) are dissimilar.

(*BSD stat uses -f for the format option, not -c, and the format specifiers are different.)

 
Old 07-31-2007, 07:17 PM   #10
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by Cathinfo
Say you have your typical

/mnt/point

and you want to know if something has been mounted on that point.

How can you do that in a Bash script?

Sometimes you want to know if the directory is "just a directory" or if it refers to an external USB drive, hard drive, etc.


Code:
NL='
'
point=$1  ##  /mnt/point
mnt=`df -P "$point"`
mnt=${mnt#*"$NL"}
case $mnt in
  *"$point") echo "${mnt%% *} is mounted on $point" ;;
  *) echo "Nothing mounted on $point" ;;
esac
 
  


Reply



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
/dev/sda1 already mounted or /mnt busy. (SATA Sil 3112) psamuel01 Red Hat 14 10-10-2008 03:59 PM
Mount "already mounted or /mnt busy" problems with dmraid, Promise FastTrak, 2.6.12 Iainuki Linux - Hardware 1 12-01-2006 08:33 AM
Where's the best place to learn Bash? evangelinux Programming 5 06-13-2004 11:45 PM
bash: cd: /mnt/floppy/: Input/output error embsupafly Linux - Hardware 1 08-16-2003 06:36 AM
umount: it seems /mnt/cdrom is mounted multiple times computergirl Linux - Hardware 13 06-23-2003 10:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:30 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
Open Source Consulting | Domain Registration