@@ -19,6 +19,10 @@ if [[ ! -f "$SETTINGS_FILE" ]]; then
fi
# Parse the JSON to get labels and corresponding details
# 临时把 IFS 设为换行,让 =($(...)) 只按行拆分、保留 label 内部空格,
# 避免默认 IFS(空格) 拆散含空格的 label,导致每台显示两行、labels/hosts 错位
oldIFS = $IFS
IFS = $'\n'
labels = ( $( jq -r '.["sshfs.configs"][] | .label' " $SETTINGS_FILE " ) )
hosts = ( $( jq -r '.["sshfs.configs"][] | .host' " $SETTINGS_FILE " ) )
ports = ( $( jq -r '.["sshfs.configs"][] | .port' " $SETTINGS_FILE " ) )
@@ -26,13 +30,12 @@ names=($(jq -r '.["sshfs.configs"][] | .name' "$SETTINGS_FILE"))
usernames = ( $( jq -r '.["sshfs.configs"][] | .username' " $SETTINGS_FILE " ) )
passwords = ( $( jq -r '.["sshfs.configs"][] | .password' " $SETTINGS_FILE " ) )
privateKeyPaths = ( $( jq -r '.["sshfs.configs"][] | .privateKeyPath' " $SETTINGS_FILE " ) )
IFS = $oldIFS
options = ( )
prefixes = ( )
for i in " ${ !labels[@] } " ; do
option = $( echo " ${ labels [ $i ] } " | sed 's/[^[:alnum:]].*//' ) # 提取前几个 字母和 数字
options += ( " $option : ${ labels [ $i ] } " ) # 去掉序号,保留前缀和完整label
prefixes += ( " $option " ) # 保存前缀
option = $( echo " ${ labels [ $i ] } " | sed 's/[^[:alnum:]].*//' ) # 提取前缀(前导 字母数字)
prefixes += ( " $option " )
done
# Check if a command-line argument is provided
@@ -40,16 +43,26 @@ if [ "$1" != "" ]; then
# 如果提供了参数,则直接使用这个前缀
host_to_ssh = $1
else
echo "Select a target to connect via SSH, or press 0 to exit:"
echo "Select a target to connect via SSH (Enter to exit) :"
echo
# Display options without numbers
for opt in " ${ options [@] } " ; do
echo " $opt "
# 对齐前缀列:按最长前缀宽度填充
maxlen = 0
for p in " ${ prefixes [@] } " ; do
[ ${# p } -gt $maxlen ] && maxlen = ${# p }
done
for i in " ${ !labels[@] } " ; do
printf "%-*s: %s\n" " $maxlen " " ${ prefixes [ $i ] } " " ${ labels [ $i ] } "
done
echo
read -p "请输入要连接的主机前缀: " host_to_ssh
read -p "请输入要连接的主机前缀 (直接回车退出) : " host_to_ssh
# 空输入直接退出(空前缀会误匹配到错乱的数组项,直接拦截)
if [ -z " $host_to_ssh " ] ; then
echo "已退出。"
exit 0
fi
fi
echo