#!/bin/bash
# 清空屏幕
tput clear
# 获取终端尺寸
rows=$(tput lines)
cols=$(tput cols)
# 计算菜单的位置和尺寸
menu_width=40
menu_height=10
menu_top=$(( (rows - menu_height) / 2 ))
menu_left=$(( (cols - menu_width) / 2 ))
# 设置绘制属性
tput bold
tput setaf 6 # 设置文本颜色为青色
# 绘制菜单边框
for ((i = 0; i < menu_height; i++)); do
tput cup $((menu_top + i)) $menu_left
for ((j = 0; j < menu_width; j++)); do
if [ $i -eq 0 ] || [ $i -eq $((menu_height - 1)) ] || [ $j -eq 0 ] || [ $j -eq $((menu_width - 1)) ]; then
echo -n "#"
else
echo -n " "
fi
done
done
# 绘制菜单标题
title="漂亮的菜单"
title_length=${#title}
title_left=$(( (menu_width - title_length) / 2 + menu_left ))
tput cup $((menu_top + 2)) $title_left
echo "$title"
# 绘制菜单选项
options=("选项 1" "选项 2" "选项 3" "退出")
num_options=${#options[@]}
options_top=$((menu_top + 4))
options_left=$((menu_left + 2))
for ((i = 0; i < num_options; i++)); do
tput cup $((options_top + i)) $options_left
echo "${options[i]}"
done
# 处理用户输入
tput cup $((options_top + num_options + 2)) $options_left
read -p "请输入选项编号: " choice
case $choice in
1)
echo "您选择了选项 1"
;;
2)
echo "您选择了选项 2"
;;
3)
echo "您选择了选项 3"
;;
*)
echo "退出菜单"
;;
esac
# 恢复绘制属性
tput sgr0
# 移动光标到屏幕底部
tput cup $rows 0
使用tput写的shell菜单
最新推荐文章于 2024-06-14 14:37:55 发布