Advertisement
hackbyte

ismounted.sh_2019-04-24T21-18-56CEST

Apr 18th, 2019
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # function ismounted("PATH")
  4. #
  5. # checks if a given PATH is actually a mount..
  6. #
  7. # v2019-04-24T21-18-56CEST (UTC+2)
  8. #   Added some bits of documentation and links for
  9. #   more in depth info. ;)
  10. #
  11.  
  12. function ismounted() {
  13.     # this funtion performs the actual test
  14.     # if a path given as parameter is actually a mount.
  15.     #
  16.     # it uses default sh/bash return codes, according to this hint:
  17.     # https://stackoverflow.com/questions/5431909/returning-a-boolean-from-a-bash-function/43840545#43840545
  18.     #
  19.     # Which means, on success, return 0,
  20.     # on a failure (is not a mount) return anything else.
  21.     #
  22.     # the cut and grep below, just grab the mounted dirs from
  23.     # /proc/self/mounts (delimeter is space and we want field number 2)
  24.     # and then filters them (with exact match) against our parameter.
  25.     #
  26.     # After that, we can simply compare the trings we got.
  27.     # It will be empty if no mount is found or it will exactly
  28.     # be the path we got as parameter...
  29.     #
  30.     # http://tldp.org/LDP/abs/html/comparison-ops.html
  31.     #
  32.     if ! [ -d "$1" ] ; then
  33.         # if our parameter is not a directory at all,
  34.         # it usually can not be a mount... ;)
  35.         return 1
  36.     fi
  37.     if [ "$(cut -d' ' -f2 /proc/self/mounts | grep -x "$1")" = "$1" ] ; then
  38.         # our parameter matches against a existing mount,
  39.         # just return with a exit status of 0
  40.         return 0
  41.     else
  42.         # our parameter got no match,
  43.         # so we return with a exit status of 1
  44.         return 1
  45.     fi
  46. }
  47.  
  48.  
  49. #
  50. # this variant just uses the exitstatus to see if
  51. # the dir is a mount.
  52. #
  53. if ismounted $1 ; then
  54.     echo "$1 is mounted!"
  55. else
  56.     echo "$1 is not mounted!"
  57. fi
  58.  
  59. #
  60. # this variant calls ismounted and _inverts_ the returned exitstatus
  61. # to get the stuff convenient for our test-case ( i need to know if mounts are _NOT_ existing)
  62. #
  63. if ! ismounted $1 ; then
  64.     echo "$1 is not mounted!"
  65. else
  66.     echo "$1 is mounted!"
  67. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement