LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 08-01-2010, 11:15 AM   #1
wael_h
Member
 
Registered: Mar 2009
Location: Phoenix, Arizona
Distribution: Slackware
Posts: 102

Rep: Reputation: 39
Check if device is mounted in a bash script


Writing a bash script and I need to check if a known HD partition is mounted or not. I do not think /etc/mtab is the place to check. Would /proc/mounts always work? To make it simple like this :

cat /proc/mounts | grep /dev/sdb2

Is there a better way? Thanks inadvance.
 
Old 08-01-2010, 11:17 AM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
You could grep through the output of "mount", /etc/mtab, or /proc/mounts. Any of those would give you the desired result rather easily.
 
Old 08-01-2010, 11:46 AM   #3
wael_h
Member
 
Registered: Mar 2009
Location: Phoenix, Arizona
Distribution: Slackware
Posts: 102

Original Poster
Rep: Reputation: 39
Quote:
Originally Posted by wael_h View Post
Writing a bash script and I need to check if a known HD partition is mounted or not. I do not think /etc/mtab is the place to check. Would /proc/mounts always work? To make it simple like this :

cat /proc/mounts | grep /dev/sdb2

Is there a better way? Thanks inadvance.
I wanted to mention that when device is unmounted it is still listed in /etc/mtab but not in /proc/mounts. By the way the device is listed in /etc/fstab with defaults option. So how can I update /etc/mtab without mounting a device that is listed in /etc/fstab?
 
Old 08-01-2010, 12:42 PM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 250Reputation: 250Reputation: 250
Why don't you try performing a simple file operation on the desired mountpoint? If it fails you'll get an error.
 
Old 08-01-2010, 12:52 PM   #5
number22
Member
 
Registered: Sep 2006
Location: Earth
Distribution: Slackware 14.1 Slackware64-current multilib
Posts: 278
Blog Entries: 7

Rep: Reputation: Disabled
check df output string is easier.
 
Old 08-01-2010, 01:05 PM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
This is a helper script I wrote for some backup software, it lists the device node,mount point and volume label I'm sure you can adapt it to your needs:
Code:
#!/bin/bash

PASS=$1
BLKID=/sbin/blkid

echo $PASS|sudo -S $BLKID -o full|while read REPLY
	do
		DEV=$(echo $REPLY|cut -d ":" -f 1 -)
		LABEL=$(echo $REPLY|awk -F LABEL= '{print $2}' - |cut -f 1 -d " " - )
		LABEL=${LABEL//\"/}
		MP=$(mount|grep $DEV|awk '{print $3'})

		if [ "X$MP" = "X" ];then
			MP="Mount=None"
		else
			MP="Mount=$MP"
		fi
		
		if [ "X$LABEL" = "X" ];then
			LABEL="Label=None"
		else
			LABEL="Label=$LABEL"
		fi
		
		
		echo "$DEV,$MP,$LABEL"
	done
An admin password is passed as the only option ie:
Code:
listdevs youradminpassword
 
  


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
Bash-Check If FS is Already Mounted On Directory fortezza Programming 15 04-27-2013 07:06 PM
i need to constantly check whether a cd is mounted or not using bash anurupr Linux - Newbie 8 03-20-2010 07:40 AM
script to check if a drive is mounted gazman1 Programming 3 07-10-2006 09:09 AM
Check for mounted device from shell seagoj Linux - Software 6 02-24-2006 10:20 AM
script check to see if device is mounted Warmduvet Programming 14 09-03-2004 06:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 06:57 AM.

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