LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   Check if device is mounted in a bash script (https://www.linuxquestions.org/questions/slackware-14/check-if-device-is-mounted-in-a-bash-script-823401/)

wael_h 08-01-2010 11:15 AM

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.

MS3FGX 08-01-2010 11:17 AM

You could grep through the output of "mount", /etc/mtab, or /proc/mounts. Any of those would give you the desired result rather easily.

wael_h 08-01-2010 11:46 AM

Quote:

Originally Posted by wael_h (Post 4051768)
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?

smoker 08-01-2010 12:42 PM

Why don't you try performing a simple file operation on the desired mountpoint? If it fails you'll get an error.

number22 08-01-2010 12:52 PM

check df output string is easier.

Keith Hedger 08-01-2010 01:05 PM

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


All times are GMT -5. The time now is 05:39 PM.