How to check if a partition is mounted or not with bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if a partition is mounted or not with bash?
# 1  
Old 10-25-2010
How to check if a partition is mounted or not with bash?

How to check if a partition is mounted or not with bash?
And when is $? variable one?
Please give example.
# 2  
Old 10-25-2010
How about
Code:
df | grep -q partition

# 3  
Old 10-25-2010
Quote:
Originally Posted by cola
How to check if a partition is mounted or not with bash?
And when is $? variable one?
Please give example.
1. You can't "mount" a partition, only a "filesystem". This differentiation might seem picky, but in fact it is an important one.

2. You can't check it's mounting status with a shell, because a shell is a means to execute programs. You can use such a program - "mount", "df", probably some more - to find out if a filesystem is mounted, but that would be irregardless of the shell used.

3. "$?" is a variable set by an exiting program and contains the error code (or "error level"). If you want to know what an error level of 1 means for a certain program have a look in that programs man page.

4. You might want to read some introductory books about the Unix OS to get some basics. It won't do you any good in the long run, if you ask questions which show a clear lack of understanding of the underlying concepts. You might not understand the answer given and gain nothing from it - even if the answer is correct.

I hope this helps.

bakunin
# 4  
Old 10-25-2010
maybe try justdoit Smilie

Code:
# ./justdoit
/dev/sda1 is mounted ->  /boot
/dev/sda2 is non-mounted
.....
.....

Code:
## justdoit ##
#!/bin/bash
for i in `fdisk -l | grep sd | sed '/^Disk/d'|sed 's/^\([^ ]*\)  *.*/\1/'`
  do
    if [ `df "$i" | sed '1d;s/\([^ ]*\)  *.*/\1/'` = '-' ] ; then
      echo "$i" is non-mounted
    else
      echo "$i is mounted -> " `df "$i" | sed '1d;s/.* \([^ ]*\)$/\1/'`
    fi
  done

# 5  
Old 10-25-2010
Quote:
Originally Posted by bakunin
1. You can't "mount" a partition, only a "filesystem". This differentiation might seem picky, but in fact it is an important one.

2. You can't check it's mounting status with a shell, because a shell is a means to execute programs. You can use such a program - "mount", "df", probably some more - to find out if a filesystem is mounted, but that would be irregardless of the shell used.

3. "$?" is a variable set by an exiting program and contains the error code (or "error level"). If you want to know what an error level of 1 means for a certain program have a look in that programs man page.

4. You might want to read some introductory books about the Unix OS to get some basics. It won't do you any good in the long run, if you ask questions which show a clear lack of understanding of the underlying concepts. You might not understand the answer given and gain nothing from it - even if the answer is correct.

I hope this helps.

bakunin
I know about:
Code:
df -h | grep <partition>
cat /proc/mounts | grep <partition>
cat /etc/mtab | grep <partition>

$? would be 0 if grep finds otherwise 1 if it doesn't find.
# 6  
Old 10-25-2010
Or grep /proc/mounts (or /etc/mtab) directly:
Code:
if grep -q <partition> /proc/mounts; then
    echo "It's mounted"
fi

# 7  
Old 10-26-2010
Quote:
Originally Posted by bakunin
2. You can't check it's mounting status with a shell, because a shell is a means to execute programs. You can use such a program - "mount", "df", probably some more - to find out if a filesystem is mounted, but that would be irregardless of the shell used.
Not true, a shell has it's on internal commands, and these can be used to process information. As a general rule shell internal commands tend to be much more efficient that external programs because the no code needs to be loaded and executed (beyond the shell it's self) to do the processing.

For example the following bash script will check if a filesystem is mounted, by reading the /proc/mounts list:

Code:
#!/bin/bash
FS="$1"
while read line
do
    [[ "$line" =~ "$FS " ]] && echo "$FS is Mounted"
done < /proc/mounts

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to check if file systems are mounted

Hi I have the following piece of code, running on a solaris 10 O.S., that is not working for NFS file systems: for vol in `grep -E 'vxfs|ufs|nfs' /etc/vfstab | egrep -v '^#' | awk '{ print $3 }'` do if df -k $vol | grep $vol > /dev/null then outputOK "Filesystem: $vol mounted" else... (1 Reply)
Discussion started by: fretagi
1 Replies

2. Shell Programming and Scripting

Help with code to check if file systems are mounted

Hi I need to have a piece of code that check if all file systems are mounted or not. I have to pieces of information like the output of the bdfcommand, and the file /etc/fstab. The first is: bdf Filesystem kbytes used avail %used Mounted on /dev/vg00/lvol3 2097152 266656... (3 Replies)
Discussion started by: fretagi
3 Replies

3. Windows & DOS: Issues & Discussions

How to check if the folders in mounted on which partition?

Hi there, I am able to check which parition from Storage > Disk Management How is it possible to check if the folder is mounted on which partition. (1 Reply)
Discussion started by: alvinoo
1 Replies

4. Solaris

Check mounted filesystems

Hi, Please help me to tell How to check mounted filesystems for any inconsistency. Can I run fsck -m /dev/rdsk/cntndnsn for this? Thanks, (3 Replies)
Discussion started by: Manmohan Mishra
3 Replies

5. Shell Programming and Scripting

Check if NAS filesystem is mounted

Anyone know the best way to check and see if a NAS filesystem is mounted on a linux box. I have no idea where to start :wall:. (2 Replies)
Discussion started by: d3mon_spawn
2 Replies

6. Shell Programming and Scripting

bash script to check if mounted, and mount if not

I'd like to make a wrapper bash script that will make sure that an nfs mount is mounted before launching a program that depends on the mount being active. Basically: 1) Check to see if the mount is active 2) If it's not active, try to mount it 3) If it won't mount because the nfs server is... (3 Replies)
Discussion started by: graysky
3 Replies

7. Shell Programming and Scripting

How to display partition name on mounted on using this script

Hi All Guys.... root> df -k|grep /u0 /dev/vx/dsk/oradg/u02vol 12582912 8717924 3744252 70% /u02 /dev/vx/dsk/oradg/u01vol 8796160 5563610 3131556 64% /u01 /dev/vx/dsk/oradg/u04vol 10035200 1247888 8519534 13% /u04 /dev/vx/dsk/oradg/u03vol 12582912 2524060 9744542 21% ... (3 Replies)
Discussion started by: adzuanamir
3 Replies

8. Solaris

swap partition showing mounted in df -h

Dear All Anyone can help me what is the problem of swap partition? swap partition is showing mounted in df -h command output. Regards prakash (1 Reply)
Discussion started by: pshelke
1 Replies

9. UNIX for Dummies Questions & Answers

Solaris10:How to check where /usr are really mounted

How can I check which partition /usr are mounted on ? Usually this is mounted on root (/). If I want to move /usr to another partition, how do I do this ? BR Ludwig (1 Reply)
Discussion started by: ludwig
1 Replies

10. SCO

/ partition is mounted but not present in mount

Hello , I 've got a problem with the root partition on my SCO 5.0.5 . When I check the disk with df or mount , I can 't see the root filesystem . # mount /stand on /dev/boot read only on Tue Sep 05 16:13:51 2006 /home on /dev/home read/write on Tue Sep 05 16:14:41 2006 But , if I try... (3 Replies)
Discussion started by: npn35
3 Replies
Login or Register to Ask a Question