Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: test if volumes are mounted using bash script

  1. #1
    Join Date
    Apr 2006
    Beans
    313

    test if volumes are mounted using bash script

    I have found the code to do this before, and now I cannot remember nor find it again. Basically I want to test if volumes are mounted from inside a bash script, and take action depending on the finding.

    Pseudocode:

    volume="/media/storage"

    if [ $volume is mounted] then
    echo "mounted"
    else
    echo "not mounted"
    fi

    Can someone please help.

  2. #2
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    Hidden!
    Distro
    Hardy Heron (Ubuntu Development)

    Re: test if volumes are mounted using bash script

    This seems related, I'm not sure if it's done correctly:
    http://forums.macosxhints.com/showthread.php?t=14156

  3. #3
    Join Date
    Jul 2006
    Location
    Denton, TX, US
    Beans
    1,355
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: test if volumes are mounted using bash script

    This follows the basic syntax you laid out.
    Code:
    volume="/media/storage"
    
    if mount|grep $volume; then
    echo "mounted"
    else
    echo "not mounted"
    fi

  4. #4
    Join Date
    Apr 2006
    Beans
    313

    Re: test if volumes are mounted using bash script

    Different than what I used before (I think there was an -e switch somewhere in the if statement), but this does what I need it to do.

    Much thanks blackened.

  5. #5
    Join Date
    Jul 2006
    Location
    Denton, TX, US
    Beans
    1,355
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: test if volumes are mounted using bash script

    As ever, there's more than one way to skin the proverbial cat.

  6. #6
    Join Date
    Aug 2005
    Location
    Cape Town
    Beans
    316
    Distro
    Kubuntu

    Re: test if volumes are mounted using bash script

    The above test could result in some false positives. For example you may want to check whether /mydata is mounted, and someone might just have a usb-disk mounted under /media/mydata. The test will pass as the "grep" will find the string "/mydata" in "/media/mydata".

    Many other possible false positives might be encountered, especially with nested mountpoints.

    A safer check would be

    Code:
    ....
    if [ mount | grep "on ${volume} type" > /dev/null ]
    then
    ...
    The entire string "on ${volume} type" needs to match and this avoids matching sub-strings.

  7. #7
    Join Date
    Apr 2006
    Beans
    313

    Re: test if volumes are mounted using bash script

    thanks hartz3, i'll try that when I get home tonight.

  8. #8
    Join Date
    Aug 2005
    Location
    Cape Town
    Beans
    316
    Distro
    Kubuntu

    Re: test if volumes are mounted using bash script

    My appologies, there is a mistake in my code. It should look like this:

    Code:
    ....
    if mount | grep "on ${volume} type" > /dev/null
    then
    ...
    else
    ...
    fi
    ... Note the missing [...] brackets.

    or alternatively

    Code:
    mount | grep "on ${volume} type" > /dev/null
    if [ $? -eq 0 ]
    then
    ...
    else
    ...
    fi

  9. #9
    Join Date
    Apr 2006
    Beans
    313

    Re: test if volumes are mounted using bash script

    thanx again.

  10. #10
    Join Date
    Mar 2009
    Beans
    1

    Re: test if volumes are mounted using bash script

    As mentioned before there are more then one solutions for a problem, so here is another one:

    libsmount="/some/potential/mount/point/to/check";

    mountpoint $libsmount
    if [ $? -eq 0 ] ; then
    echo "already mounted"
    else
    echo "not mounted do it now"
    fi

    Cheers

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •