shell脚本中常用的菜单模板

本文介绍了一个使用Bash编写的菜单脚本示例,该脚本利用tput命令进行美化显示,包括版本信息、标题及选项列表,并提供用户交互功能。

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

直接上代码:

#!/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的相关知识,在另一篇帖子中讲解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值