A collection of scripts and configurations for Bash (and others)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

137 行
2.6KB

  1. #!/bin/bash
  2. # These are all default values
  3. REMOTE_IN_SSHCONFIG=-1
  4. REMOTE_USER="obsidianblk"
  5. REMOTE_HOST="Trappist1"
  6. REMOTE_PATH="/home/obsidianblk"
  7. REMOTE_PORT=22
  8. MOUNT_POINT="$HOME/remote"
  9. PORT_VAR=""
  10. NEXT_ARG_IS_FILE=-1
  11. OP=""
  12. SRC_FILE=""
  13. URI=""
  14. function usage() {
  15. echo "Usage: $0 [-o <path to mount options>] mount|unmount|remount"
  16. }
  17. function defineURI() {
  18. URI="$REMOTE_HOST:$REMOTE_PATH"
  19. if [ $REMOTE_IN_SSHCONFIG -lt 0 ]; then
  20. URI="$REMOTE_USER@$URI"
  21. PORT_VAR="-p $REMOTE_PORT"
  22. fi
  23. }
  24. if [[ "$#" -lt 1 ]]; then
  25. echo "Missing operation argument."
  26. usage
  27. exit 1
  28. fi
  29. for arg in "$@"; do
  30. if [ $NEXT_ARG_IS_FILE -ge 0 ]; then
  31. if [ -f "$arg" ]; then
  32. SRC_FILE="$arg"
  33. source "$arg"
  34. NEXT_ARG_IS_FILE=-1
  35. else
  36. echo "ERROR: Option file $arg missing or invalid."
  37. usage
  38. exit 1
  39. fi
  40. else
  41. case "$arg" in
  42. mount) ;&
  43. unmount) ;&
  44. remount)
  45. if [ "$OP" != "" ]; then
  46. echo "ERROR: Multiple operations defined."
  47. usage
  48. exit 1
  49. fi
  50. OP="$arg"
  51. ;;
  52. -o)
  53. NEXT_ARG_IS_FILE=0
  54. ;;
  55. esac
  56. fi
  57. done
  58. if ! [[ -d "$MOUNT_POINT" ]]; then
  59. echo "ERROR: [$MOUNT_POINT] does not exist or is not a directory."
  60. usage
  61. exit 1
  62. fi
  63. case "$OP" in
  64. mount)
  65. defineURI
  66. if ! mount | grep "$URI on $MOUNT_POINT" > /dev/null
  67. then
  68. echo "Mounting $URI on $MOUNT_POINT... "
  69. sshfs -C $PORT_VAR -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 $URI $MOUNT_POINT &&
  70. ( # Success
  71. echo "Mounted."
  72. notify-send -i server 'Successfully Mounted' "$URI" &> /dev/null
  73. exit 0
  74. ) ||
  75. ( # Failure
  76. echo "Mount Failed!"
  77. notify-send -i error 'Failed to mount' "$URI" &> /dev/null
  78. exit 1
  79. )
  80. else
  81. echo "Remote folder is already mounted."
  82. exit 1
  83. fi
  84. ;;
  85. unmount)
  86. defineURI
  87. echo "$URI"
  88. if mount | grep "$URI on $MOUNT_POINT" > /dev/null
  89. then
  90. echo "Unmounting $URI from $MOUNT_POINT... "
  91. fusermount -u $MOUNT_POINT &&
  92. ( # Success
  93. echo "Unmount Successful."
  94. notify-send -i server 'Successfully Unmounted' "$URI" &> /dev/null
  95. exit 0
  96. ) ||
  97. ( # Failure
  98. echo "FAILED to Unmount!"
  99. notify-send error 'Failed to unmount' "$URI" &> /dev/null
  100. exit 1
  101. )
  102. else
  103. echo "Remote folder not mounted."
  104. exit 1
  105. fi
  106. ;;
  107. remount)
  108. $SRC_ARG=""
  109. if $SRC_FILE != ""; then
  110. SRC_ARG="-o $SRC_FILE"
  111. fi
  112. $0 unmount $SRC_ARG $OP &&
  113. $0 mount $SRC_SRG $OP
  114. ;;
  115. *)
  116. usage
  117. exit
  118. ;;
  119. esac