bash上下键选择选项demo脚本

效果如下:

废话不多说,上代码:

bash 复制代码
#!/bin/bash

options=("111" "222" "333" "444")  # 选项列表
options_index=0  # 默认选中第一个选项
options_len=${#options[@]}


echo "请用上下方向键进行选择,空格键选中/取消,回车键确认结果"

# 定义一个数组来存储选中的结果
selected=()
for ((i=0; i<${options_len}; i++));do
    selected[$i]=0
done

# 渲染选项列表
render_options() {
    for i in "${!options[@]}"; do
        # 首先渲染已经选中的选项
        if [ ${selected[$i]} -eq 1 ];then
            if [ $i -eq $options_index ]; then
                echo -e "\033[1;41;34m${options[$i]}\033[0m"  # 已选中,已选择
            else
                echo -e "\033[1;41;33m${options[$i]}\033[0m"  # 已选中,未选择
            fi
        elif [ $i -eq $options_index ]; then
            echo -e "\033[1;34m${options[$i]}\033[0m"  # 未选中,已选择
        else
            echo "${options[$i]}"  # 未选中,未选择
        fi
    done
}

# 初始渲染
render_options

# 为了让read能读到空格键
IFS_store=$IFS
IFS=''

while true; do
    read -s -n 1 key  # 读取单个按键输入,不显示在终端上

    case $key in
        "A")  # 上箭头键
            if [ $options_index -gt 0 ]; then
                options_index=$((options_index - 1))
            # 在第一行按上键,到最后一行
            elif [ $options_index -eq 0 ]; then
                options_index=$((${options_len} - 1))
            fi
            ;;
        "B")  # 下箭头键
            if [ $options_index -lt $(( ${options_len} - 1 )) ]; then
                options_index=$((options_index + 1))
            # 在最后一行按下键,到第一行
            elif [ $options_index -eq $(( ${options_len} - 1 )) ]; then
                options_index=0
            fi
            ;;
        " ")  # 空格键
            # selected[$options_index]的值,0、1切换
            selected[$options_index]=$((1 - ${selected[$options_index]}))
            ;;
        "")  # 回车键
            break
            ;;
    esac

    tput cuu ${options_len}  # 光标移动回到选项列表的开头
    tput ed  # 清除当前行
    render_options  # 重新渲染选项列表
done

# 恢复IFS变量
IFS=$IFS_store

# 最后选中的所有结果
result=()
for ((i=0; i<options_len; i++));do
    if [ ${selected[$i]} -eq 1 ];then
        result+=(${options[$i]})
    fi
done

echo "选中:${result[@]}"
相关推荐
aramae17 分钟前
数据结构与算法(递归)
开发语言·经验分享·笔记·算法
千码君201625 分钟前
Go语言:记录一下Go语言系统学习的第一天
java·开发语言·学习·golang·gin·并发编程·编译语言
Lxinccode1 小时前
python(48) : 命名截图[Windows工具(3)]
开发语言·python·截图·快速截图
骁的小小站1 小时前
Learn C the Hardway学习笔记和拓展知识(一)
c语言·开发语言·c++·经验分享·笔记·学习·bash
花哥码天下1 小时前
Oracle下载JDK无需登录
java·开发语言
早点.早点.2 小时前
QT登陆界面
开发语言·qt
楼田莉子2 小时前
C++学习:异常及其处理
开发语言·c++·学习·visual studio
fsnine2 小时前
Python Web框架对比与模型部署
开发语言·前端·python
海梨花2 小时前
【八股笔记】SSM
java·开发语言·笔记·后端·面试·框架