Files
Linux/File/Shell/button.sh
offends cee91802b3
Some checks failed
continuous-integration/drone Build is failing
synchronization
2025-08-25 15:57:40 +08:00

74 lines
1.8 KiB
Bash

#!/bin/bash
#############################################################################################
# 用途: Shell模拟按钮
# 作者: 丁辉
# 编写时间: 2024-05-20
#############################################################################################
function Button() {
# 定义选项列表
options=("选项一" "选项二")
# 初始化选项索引
selected_index=0
# 清除屏幕
clear
# 显示选项列表
display_options() {
for i in "${!options[@]}"; do
if [ $i -eq $selected_index ]; then
echo "> ${options[i]}"
else
echo " ${options[i]}"
fi
done
}
# 循环直到用户选择退出
while true; do
display_options
# 读取用户输入
read -r -sn 1 key
# 处理用户输入
case $key in
"A") # 上箭头
if [ $selected_index -gt 0 ]; then
selected_index=$((selected_index - 1))
fi
;;
"B") # 下箭头
if [ $selected_index -lt $(( ${#options[@]} - 1 )) ]; then
selected_index=$((selected_index + 1))
fi
;;
""|" ") # 回车键或空格键
break
;;
"q") # 按 'q' 键退出
clear
echo "退出"
exit
;;
esac
# 清除屏幕
clear
done
# 用户选择的选项
selected_option="${options[selected_index]}"
send_info $selected_option
if [ "$selected_option" == "选项一" ]; then
echo "选项一"
elif [ "$selected_option" == "选项二" ]; then
echo "选项二"
fi
}
Button