Files
myScripts/mount-all.sh
2026-05-19 22:27:05 +00:00

164 lines
5.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# ============================================================
# SSHFS Manager
# 要新增或修改的話,就只要編輯這裡:
# 格式:"名稱|使用者@主機:遠端路徑|本地掛載點"
# ============================================================
ENTRIES=(
"janet-mac-mini | janet@192.168.1.103:/ | $HOME/mnt/janet-mac-mini"
"nas2 | wiwi@192.168.1.114:/ | $HOME/mnt/nas2"
"nas3 | wiwi@192.168.1.111:/ | $HOME/mnt/nas3"
"99 | wiwi@192.168.1.99:/ | $HOME/mnt/99"
"88 | wiwi@192.168.1.88:/ | $HOME/mnt/88"
"nas1 | wiwi@192.168.1.105:/ | $HOME/mnt/nas1"
)
# ============================================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
RESET='\033[0m'
get_name() { echo "$1" | awk -F'|' '{gsub(/ /,"",$1); print $1}'; }
get_remote(){ echo "$1" | awk -F'|' '{gsub(/^ +| +$/,"",$2); print $2}'; }
get_local() { echo "$1" | awk -F'|' '{gsub(/^ +| +$/,"",$3); print $3}'; }
is_mounted() { mountpoint -q "$1" 2>/dev/null; }
print_status() {
echo ""
echo -e "${BOLD} # 狀態 名稱${RESET}"
echo " ─────────────────────────────"
local i=1
for entry in "${ENTRIES[@]}"; do
local name remote local_path
name=$(get_name "$entry")
local_path=$(get_local "$entry")
remote=$(get_remote "$entry")
if is_mounted "$local_path"; then
printf " ${BOLD}%d${RESET} ${GREEN}✔ 已掛${RESET} %-20s ${CYAN}%s${RESET}\n" "$i" "$name" "$remote"
else
printf " ${BOLD}%d${RESET} ${RED}✘ 未掛${RESET} %-20s ${CYAN}%s${RESET}\n" "$i" "$name" "$remote"
fi
((i++))
done
echo ""
}
do_mount() {
local entry="$1"
local name remote local_path
name=$(get_name "$entry")
remote=$(get_remote "$entry")
local_path=$(get_local "$entry")
if is_mounted "$local_path"; then
echo -e "${YELLOW}$name 已經掛載了,跳過。${RESET}"
return
fi
mkdir -p "$local_path"
echo -e "掛載 ${BOLD}$name${RESET} ($remote$local_path) ..."
if sshfs "$remote" "$local_path"; then
echo -e "${GREEN}✔ 成功${RESET}"
else
echo -e "${RED}✘ 失敗${RESET}"
fi
}
do_umount() {
local entry="$1"
local name local_path
name=$(get_name "$entry")
local_path=$(get_local "$entry")
if ! is_mounted "$local_path"; then
echo -e "${YELLOW}$name 本來就沒掛載,跳過。${RESET}"
return
fi
echo -e "卸載 ${BOLD}$name${RESET} ..."
if fusermount -u "$local_path"; then
echo -e "${GREEN}✔ 成功${RESET}"
else
echo -e "${RED}✘ 失敗${RESET}"
fi
}
parse_selection() {
local input="$1"
local max="$2"
local result=()
IFS=',' read -ra parts <<< "$input"
for part in "${parts[@]}"; do
part=$(echo "$part" | tr -d ' ')
if [[ "$part" =~ ^[0-9]+-[0-9]+$ ]]; then
local start="${part%-*}" end="${part#*-}"
for ((n=start; n<=end && n<=max; n++)); do result+=("$n"); done
elif [[ "$part" =~ ^[0-9]+$ ]] && (( part >= 1 && part <= max )); then
result+=("$part")
fi
done
echo "${result[@]}"
}
# ── 主迴圈 ──────────────────────────────────────────────────
clear
while true; do
echo -e "${BOLD}╔══════════════════════════════╗${RESET}"
echo -e "${BOLD}║ SSHFS Manager ║${RESET}"
echo -e "${BOLD}╚══════════════════════════════╝${RESET}"
print_status
echo -e " ${BOLD}m[編號]${RESET} 掛載 ${BOLD}u[編號]${RESET} 卸載 ${BOLD}a${RESET} 全掛 ${BOLD}x${RESET} 全卸 ${BOLD}q${RESET} 離開"
echo -e " (例如:${BOLD}m5${RESET}${BOLD}m1-3${RESET}${BOLD}u1,4,5${RESET}"
echo ""
read -rp " 選擇操作:" action
# 把 action 拆成「指令字母」和「後面的參數」
cmd="${action:0:1}"
arg="${action:1}"
case "$cmd" in
q|Q) echo "掰~"; exit 0 ;;
a|A)
for entry in "${ENTRIES[@]}"; do do_mount "$entry"; done
;;
x|X)
for entry in "${ENTRIES[@]}"; do do_umount "$entry"; done
;;
m|M)
if [[ -z "$arg" ]]; then
print_status
echo -e " 要掛載哪幾個?(輸入編號,可用逗號或範圍,例如 1,3 或 1-3"
read -rp " > " arg
fi
indices=$(parse_selection "$arg" "${#ENTRIES[@]}")
for i in $indices; do
do_mount "${ENTRIES[$((i-1))]}"
done
;;
u|U)
if [[ -z "$arg" ]]; then
print_status
echo -e " 要卸載哪幾個?(輸入編號,可用逗號或範圍,例如 1,3 或 1-3"
read -rp " > " arg
fi
indices=$(parse_selection "$arg" "${#ENTRIES[@]}")
for i in $indices; do
do_umount "${ENTRIES[$((i-1))]}"
done
;;
*)
echo -e "${RED}看不懂,請輸入 m / u / a / x / q${RESET}"
sleep 1 ;;
esac
done