#!/usr/bin/env bash # # Copyright 2013 Victor Penso # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # VERSION=0.1 SCRIPT=${0##*/} _error() { echo 1>&2 "Error: $@" echo "See \`$SCRIPT help\` for usage." exit 1 } HELP=\ "Usage: $SCRIPT command [arguments] Commands: list List all mounted directories. mount [user@]host:[path] [path] [options] Mount a remote directory to a local path. umount path Unmount remote directories." # Check for dependencies for cmd in sshfs ; do # If missing alert and exit type -p $cmd &>/dev/null || _error "Please install command [$cmd]." done ssh_mount() { # at least to parameters are required test $# -lt 2 && _error "Missing arguments!" local remote=$1 ; shift local path=$1 ; shift # create mount point if missing, or exit on error ( test -d $path || mkdir -p $path ) || exit 1 sshfs $remote $path -C -o reconnect,auto_cache,follow_symlinks "$@" } ssh_umount() { local path=$1 # exit if user did not provide a mount point to umount test -z "$path" && _error "Missing path to mount point!" # if mounted, umount ( mount | grep $path &>/dev/null ) && fusermount -u $path } # Read the command-line arguments _command=$1 ; shift case "$_command" in "list") mount | grep fuse.sshfs | cut -d' ' -f-3 ;; "mount") ssh_mount "$@" ;; "umount") ssh_umount "$@" ;; *) echo "$HELP" ;; esac exit 0