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.

How about

df | grep -q partition
  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

maybe try justdoit :slight_smile:

# ./justdoit
/dev/sda1 is mounted ->  /boot
/dev/sda2 is non-mounted
.....
.....
## 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

I know about:

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.

Or grep /proc/mounts (or /etc/mtab) directly:

if grep -q <partition> /proc/mounts; then
    echo "It's mounted"
fi

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:

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

using /proc/mounts would be the most accurate on a Linux system, since it gives the most up-to-date information even in a ro remount situation. There is a catch though, since modern Linux distributions mount by uuid for example and then that would no longer fly.

$ cat /proc/mounts
rootfs / rootfs rw 0 0
none /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
none /proc proc rw,nosuid,nodev,noexec,relatime 0 0
none /dev devtmpfs rw,relatime,size=504128k,nr_inodes=126032,mode=755 0 0
none /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
/dev/disk/by-uuid/e0ec44a0-e7a4-4ddb-9e3c-a0c7f0f2b5bd / ext4 rw,relatime,errors=remount-ro,barrier=1,data=ordered 0 0
[..]

This is only because in all your cited examples "grep" is the last part of the pipeline and on most systems you get the exit status of the last executed program as the exit status of the pipeline. If your list would be very long and you would execute:

df -h | grep <partition> | more

you would get the exit status of "more" instead of the exit status of "grep". This is exactly what i told you: for every program you will have to look up the meaning of its various exit statuses in its man page. This is true for "grep" as well as "more" as for any other program.

ahh, yes: wake me up when you find an internal shell command for listing mounts.

I am so sorry, I want to apologize: i want to apologize for not recognizing that the thread opener has a Linux system despite he never said so, so that your argument gains any value.

I want to apologize for saying something which is only true "in general" and maybe not for some obscure derivate of Unix. The script you provided as example is based on the existence of a "/proc" file system, which is simply not there in many Unix variants. It is also based on the existence of "/proc/mounts", which doesn't exist in almost any Unix derivate.

I foremost want to apologize for having confused the vast world of some specific Linux distribution (for which your code will probably work) and kernel version with the narrow, almost non-existent and certainly negligeable niche of all the other Unix products (in which your code won't work at all).

It was all my fault.

bakunin

It does sound like linux -
anyway, consider

#linux
mount -l | grep -q [mountpoint] 
# some other flavors of unix
mount -P | grep -q [mountpoint]  

This will return a status ($?) to check. The -l is the character ell.

I've found that sarcasm is less effective than I expected it to be when I've used it, and certainly less appreciated.