whiptai列出ls查询到的结果的数组作为选项
可以使用xargs和whiptail结合起来实现此功能。以下是一个示例脚本:
#!/bin/bash
# 获取目录中的文件列表
files=($(ls))
# 使用xargs将文件列表转换为whiptail选项格式
options=$(echo "${files[@]}" | xargs -n1 | awk '{print NR, $0, "off"}')
# 显示whiptail对话框并保存用户选择
choices=$(whiptail --title "File Selection" --checklist \
"Choose files to work with:" 20 78 10 $options 3>&1 1>&2 2>&3)
# 处理用户选择
if [ $? -eq 0 ]; then
echo "User selected: $choices"
else
echo "User cancelled."
fi
该脚本将目录中的文件列表存储在变量$files中。它使用xargs将文件列表转换为whiptail选项格式,并将结果存储在变量$options中。最后,它显示一个whiptail对话框,让用户勾选他们想要处理的文件,并将用户选择保存在变量$choices中。如果用户取消选择,则脚本会输出"User cancelled.",否则,它会输出"User selected: $choices",其中$choices是用户选择的文件列表
原文地址: http://www.cveoy.top/t/topic/eZCq 著作权归作者所有。请勿转载和采集!