直接上代码:
#!/bin/bash
# 菜单模板
function menu(){
: '
参数1:版本信息
参数2:名称
参数3:操作项列表
'
# 清屏
tput clear
# 将光标移动至(3,15)处
tput cup 3 15
# Set a foreground colour using ANSI escape
# 展示版本信息
tput setaf 3
echo $1
tput sgr0
# 展示标题
tput cup 5 17
tput rev # 反向终端
tput bold
echo $2
tput sgr0
mark=7
# 遍历带空格的字符串数组时,迭代项需要加引号
for option in "${options[@]}"
do
tput cup $mark 15
echo $option
((mark++))
done
# 继续下一个行设置输入提示消息,下一行以当前的mark值为准
tput bold
tput cup $mark 15
read -p "Enter your choice->" choice
tput clear
tput sgr0
tput rc
}
# 测试程序
function test(){
versioninfo="by howie"
title="分支选择"
options=("1. develop"
"2. release"
"q. 退出")
menu "$versioninfo" "$title" $options
}
这里用到tput的相关知识,在另一篇帖子中讲解