Linux Shell 脚本:使用 whiptail 展示 plugins 文件夹选项
使用 whiptail 在 Linux Shell 中展示 plugins 文件夹选项
本脚本使用 whiptail 工具,在终端中展示 plugins 文件夹下的所有子文件夹,并允许用户选择其中一个。脚本兼容不同数量的子文件夹,并提供简洁易懂的代码示例。
#!/bin/bash
# 获取plugins文件夹下的所有文件夹列表
plugins=$(find ~/plugins -maxdepth 1 -type d 2>/dev/null)
# 将文件夹列表转换为数组
plugins_array=()
while IFS= read -r line; do
plugins_array+=('$line' '')
done <<< "$plugins"
# 使用whiptail展示单选框
selected_plugin=$(whiptail --title '选择插件' --radiolist '请选择一个插件:' 10 50 2 "${plugins_array[@]}" 3>&1 1>&2 2>&3)
# 打印选中的插件
echo '您选择了插件:'$selected_plugin'
脚本说明:
- 获取文件夹列表:
find ~/plugins -maxdepth 1 -type d 2>/dev/null命令获取plugins文件夹下的所有子文件夹,并将结果存储在plugins变量中。 - 转换为数组: 使用
while循环将plugins变量中的每个文件夹名称转换为数组plugins_array中的一个元素。 - 使用 whiptail 展示单选框:
whiptail命令创建一个单选框,选项来自plugins_array数组。用户选择一个选项后,其值将存储在selected_plugin变量中。 - 打印选择结果:
echo命令将选择的插件名称输出到终端。
兼容性:
该脚本可以兼容任何数量的子文件夹。如果 plugins 文件夹中只有一个子文件夹,单选框将只显示一个选项。如果有多个子文件夹,单选框将自动调整大小以显示所有选项。
注意:
该脚本假设 plugins 文件夹位于用户的家目录下,并且仅包含文件夹。如果 plugins 文件夹位于其他位置或包含其他类型的文件,需要修改脚本以适应您的具体情况。
原文地址: https://www.cveoy.top/t/topic/o6HT 著作权归作者所有。请勿转载和采集!