Linux shell编程学习笔记30:打造彩色的选项菜单

简介: Linux shell编程学习笔记30:打造彩色的选项菜单

1 需求分析

Linux shell编程学习笔记21:用select in循环语句打造菜单

中,我们利用select in循环语句打造的菜单中,菜单项都是用系统设置的颜色配置来显示的。

为了让菜单更美观,我们想让每个菜单项分别以不同的颜色来显示,要如何实现呢?

2 打造彩色的选项菜单的方法

首先,我们把要显示的菜单项保存到一个数组中

然后,我们用循环语句 for in 来显示数组中的菜单项,在显示菜单时我们可以为每个菜单项加上一个数字序号作为选择该菜单的数字,并利用\e[xm (其中x的值为31-37)来实现彩色显示

接着,我们用read命令读取用户输入的数值。

最后,对用户输入的数值进行判断,并做相应的处理。

3 演示代码

我们显示一个水果菜单,并增加一个exit菜单选项

当用户输入数字n后,我们进行判断:

如果n 为水果菜单选项对应的数字,显示相应的水果

如果n为exit菜单选项的数字,显示用户选择了退出选项

否则,显示用户输入了错误的数字。

代码如下:

f=(apple banana orrange peach);# 定义水果数组
c=31;   # 颜色
o=1;    #序号
for i in $f;do
     echo -e "\e[${c}m $o)$i \e[0m"; #用不同颜色显示水果选项
     let c+=1;
     if [[ $c > 37 ]]; then
         $c=31;
     fi;
     let o+=1;
done;
     
echo " $o)exit"; # 显示 退出 选项

echo -n "enter the number before the fruit your liked(1 - ${#f}):"; #提示用户输入选项前的数字
read s; #读取用户输入的字符并存入s

if [[  $s -gt 0 && $s -le ${#f} ]]; then #用户输入的是水果选项
   let c=30+s;       #计算水果选项对应的颜色
   echo -e "You selected \e[${c}m$f[$s]\e[0m";#显示用户选择的水果选项
elif [[ $s -eq ((${#f}+1)) ]]; then # 用户选择的是退出选项
   echo You selected exit;#显示用户选择了退出选项
else
   echo Your enter a error number;#显示用户输入的错误的数字
fi

3.1 创建 脚本文件a.sh

用cp 命令来创建脚本文件,文件内容输入完后按Ctrl+D结束文件输入。

purpleEndurer @zsh $ cp /dev/stdin a.sh         
 f=(apple banana orrange peach);# 定义水果数组
 c=31;   # 颜色
 o=1;    #序号
 for i in $f;do
      echo -e "\e[${c}m $o)$i \e[0m"; #用不同颜色显示水果选项
      let c+=1;
      if [[ $c > 37 ]]; then
          $c=31;
      fi;
      let o+=1;
 done;
      
 echo " $o)exit"; # 显示 退出 选项echo -n "enter the number before the fruit your liked(1 - ${#f}):"; #提示用户输入选项前的数字
 read s; #读取用户输入的字符并存入sif [[  $s -gt 0 && $s -le ${#f} ]]; then #用户输入的是水果选项
    let c=30+s;       #计算水果选项对应的颜色
    echo -e "You selected \e[${c}m$f[$s]\e[0m";#显示用户选择的水果选项
 elif [[ $s -eq ((${#f}+1)) ]]; then # 用户选择的是退出选项
    echo You selected exit;#显示用户选择了退出选项
 else
    echo Your enter a error number;#显示用户输入的错误的数字
 fi 
 purpleEndurer @zsh $

image.png

3.2  执行和测试脚本

purpleEndurer @zsh $. ./a.sh
  1)apple 
  2)banana 
  3)orrange 
  4)peach 
  5)exit
 enter the number before the fruit your liked(1 - 4):3
 You selected orrange
 purpleEndurer @zsh $. ./a.sh
  1)apple 
  2)banana 
  3)orrange 
  4)peach 
  5)exit
 enter the number before the fruit your liked(1 - 4):5
 You selected exit
 purpleEndurer @zsh $. ./a.sh
  1)apple 
  2)banana 
  3)orrange 
  4)peach 
  5)exit
 enter the number before the fruit your liked(1 - 4):0
 Your enter a error number
 purpleEndurer @zsh $. ./a.sh
  1)apple 
  2)banana 
  3)orrange 
  4)peach 
  5)exit
 enter the number before the fruit your liked(1 - 4):9
 Your enter a error number
 purpleEndurer @zsh $

image.png

相关文章
|
2天前
|
并行计算 Ubuntu Linux
Ubuntu学习笔记(三):Linux下操作指令大全
Ubuntu学习笔记,介绍了Linux操作系统中常用的命令和操作,如文件管理、系统信息查看、软件安装等。
13 3
|
18天前
|
Shell Linux
Linux shell编程学习笔记82:w命令——一览无余
Linux shell编程学习笔记82:w命令——一览无余
|
22天前
|
Shell Linux Python
python执行linux系统命令的几种方法(python3经典编程案例)
文章介绍了多种使用Python执行Linux系统命令的方法,包括使用os模块的不同函数以及subprocess模块来调用shell命令并处理其输出。
17 0
|
2天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
108 65
|
2天前
|
Web App开发 网络协议 Linux
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
这篇文章是关于Linux命令的总结,涵盖了从基础操作到网络配置等多个方面的命令及其使用方法。
20 1
linux命令总结(centos):shell常用命令汇总,平时用不到,用到就懵逼忘了,于是专门写了这篇论文,【便持续更新】
|
4天前
|
监控 安全 网络协议
|
6天前
|
应用服务中间件 Linux nginx
Linux下操作Nginx相关命令
Linux下操作Nginx相关命令
|
7天前
|
Ubuntu 前端开发 Linux
Linux apt 命令
10月更文挑战第1天
20 4
|
7天前
|
缓存 前端开发 Linux
Linux yum 命令
10月更文挑战第1天
20 2