Find the answer to your Linux question:
Results 1 to 7 of 7
What is the best way to check if a device is mounted in a shell script? Ideally it would check if the device is mounted to the correct mount point; ...
  1. #1
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132

    Best Way to Check if Device is Mounted

    What is the best way to check if a device is mounted in a shell script?

    Ideally it would check if the device is mounted to the correct mount point; I would prefer something as basic as possible.


    Also, what happens when files are written to a directory that is a mount point while the device isn't mounted? Does the mounted device just replace those files while it is mounted, or do the files eventually get written to the device itself?
    Last edited by egan; 11-22-2009 at 09:43 PM.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    just use command mount
    it will show you everything

    if you put something into a directory while it is not mounted, it will be stored on the parent mount point (usually /), it will not get put on that device automatically, and when the device is mounted the file will look like it has disappeared

    in order to put it onto the mounted device, you will have to move it elsewhere, mount the device, then put it on the device

  3. #3
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Thanks for clearing up my file question, but I suppose I was a bit unclear.

    I want a simple algorithm that I use as a function in a bash script to return true if the prescribed directory has the prescribed device mounted to it, and false otherwise. I would prefer not to have the cut and sed out the information from the dump of human-readable information, but rather just a simple output if one exists.

  4. #4
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Anyone have any suggestions? I could hack through ouput of mount but there has to be a more elegant way to do this!

  5. #5
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    this is the logic the high level language use not the script way , i think

  6. #6
    Linux Enthusiast KenJackson's Avatar
    Join Date
    Jun 2006
    Location
    Maryland, USA
    Posts
    506
    Use /bin/mountpoint!

    Code:
    if ! mountpoint -q /mnt/backup; then
        mount /mnt/backup
    fi

  7. #7
    Linux Newbie egan's Avatar
    Join Date
    Feb 2009
    Location
    Mountain View, CA
    Posts
    132
    Quote Originally Posted by KenJackson View Post
    Use /bin/mountpoint!

    Code:
    if ! mountpoint -q /mnt/backup; then
        mount /mnt/backup
    fi
    Thank you for this! It will suffice. For some reason all the searches I did told me to awk out the data from a mount or fdisk dump which is far from elegant.

Posting Permissions

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