bash上下键选择选项demo脚本

本文介绍了一个使用bash编写的脚本,通过接收用户键盘输入(上/下箭头、空格和回车),实现在给定选项列表中进行选择,并实时渲染选中状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果如下:

 废话不多说,上代码:

#!/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[@]}"


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值