Skip to content

Instantly share code, notes, and snippets.

@fleger
Created February 20, 2011 10:39
Show Gist options
  • Save fleger/835881 to your computer and use it in GitHub Desktop.
Save fleger/835881 to your computer and use it in GitHub Desktop.
Mount fuse filesystems in one mountpoint using or logic
#! /bin/bash
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
readonly FUSE_OR_BIN="$0"
# fuse-or.mount template mountPoint root
fuse-or.mount() {
local mountPoint="${2//|/\|}"
local rootDir="${3//|/\|}"
$(echo "$1" | sed -r \
-e "s,(^|[^@])@m@([^@]|$),\1$mountPoint\2,g" \
-e "s,(^|[^@])@r@([^@]|$),\1$rootDir\2,g" \
-e "s|@@r@@|@r@|g" \
-e "s|@@m@@|@m@|g"
)
}
# fuse-or.main template mountPoint root1 [root2...]
fuse-or.main() {
if (( $# < 4 )); then
cat << EOF
Usage: $FUSE_OR_BIN template mountPoint root1 [root2]...
Arguments:
template mount program template. @m@ is replaced by the destination mountpoint and @r@ by the source root directory of the mountpoint
mountPoint destination mount point
root source root directory
Example
Try mounting through ssh in \$HOME/media/remotedata myserver:/data. If it fails, try mounting myserver.local:/data and so on:
$FUSE_OR_BIN 'sshfs @r@ @m@' "\$HOME/media/remotedata" "myserver:/data" "myserver.local:/data" "192.168.0.2:/data"
EOF
return 2
fi
# Read arguments
local template="$1"
local mountPoint="$2"
shift 2
local result=1
# Loop through arguments until the first success
until (( $# <= 0 )) || (fuse-or.mount "$template" "$mountPoint" "$1" && echo "$1 on $mountPoint succeeded" && result=0); do
echo "$1 on $mountPoint failed"
shift
done
return $result
}
fuse-or.main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment