当前位置:网站首页>Shell脚本-select in循环
Shell脚本-select in循环
2022-07-01 08:36:00 【小蜗牛的路】
select in 是 Shell 独有的一种循环
用法如下:
select variable in value_list
do
statements
done
variable 表示变量,value_list 表示取值列表,in 是 Shell 中的关键字。
代码1
#!/bin/bash
echo "What is your favourite OS?"
select name in "Linux" "Windows" "Mac OS" "UNIX" "Android"
do
echo "You have selected $name"
done
运行脚本显示如下:
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) UNIX
5) Android
#?
输入数字显示如下:
#? 3
You have selected Mac OS
#? 4
You have selected UNIX
#? 7
You have selected
#?
输入Ctrl+D 组合键,退出
注意,select 是无限循环(死循环),输入空值,或者输入的值无效,都不会结束循环,只有遇到 break 语句,或者按下 Ctrl+D 组合键才能结束循环。
代码2:select in 通常和 case in 一起使用
#!/bin/bash
echo "What is your favourite OS?"
select name in "Linux" "Windows" "Mac OS" "UNIX" "Android"
do
case $name in
"Linux")
echo "Linux是一个类UNIX操作系统,它开源免费,运行在各种服务器设备和嵌入式设备。"
break
;;
"Windows")
echo "Windows是微软开发的个人电脑操作系统,它是闭源收费的。"
break
;;
"Mac OS")
echo "Mac OS是苹果公司基于UNIX开发的一款图形界面操作系统,只能运行与苹果提供的硬件之上。"
break
;;
"UNIX")
echo "UNIX是操作系统的开山鼻祖,现在已经逐渐退出历史舞台,只应用在特殊场合。"
break
;;
"Android")
echo "Android是由Google开发的手机操作系统,目前已经占据了70%的市场份额。"
break
;;
*)
echo "输入错误,请重新输入"
esac
done
输出
输入正确选项:
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) UNIX
5) Android
#? 2
Windows是微软开发的个人电脑操作系统,它是闭源收费的。
输入错误选项:
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) UNIX
5) Android
#? 7
输入错误,请重新输入
#? 4
UNIX是操作系统的开山鼻祖,现在已经逐渐退出历史舞台,只应用在特殊场合。
输入空值:
What is your favourite OS?
1) Linux
2) Windows
3) Mac OS
4) UNIX
5) Android
#?
1) Linux
2) Windows
3) Mac OS
4) UNIX
5) Android
#? 3
Mac OS是苹果公司基于UNIX开发的一款图形界面操作系统,只能运行与苹果提供的硬件之上。
边栏推荐
- It is designed with high bandwidth, which is almost processed into an open circuit?
- Share 7 books I read in the first half of 2022
- Redis publish subscription
- Computer tips
- In depth learning training sample amplification and tag name modification
- Matlab tips (23) matrix analysis -- simulated annealing
- Gateway-88
- Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- Mavros sends a custom topic message to Px4
猜你喜欢

SPL installation and basic use (II)

【MFC开发(16)】树形控件Tree Control

There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way

vscode自定义各个区域的颜色

电脑小技巧

2022.2.15

Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
![[untitled]](/img/40/560c597d53d27eff860644cd7c3030.png)
[untitled]

Principle and application of single chip microcomputer - off chip development

factory type_id::create过程解析
随机推荐
Introduction to R language
C basic knowledge review (Part 4 of 4)
Redis源码学习(29),压缩列表学习,ziplist.c(二)
AVL树的理解和实现
【MFC开发(17)】高级列表控件List Control
IT 技术电子书 收藏
Nacos - Configuration Management
个人装修笔记
【MFC开发(16)】树形控件Tree Control
为什么LTD独立站就是Web3.0网站!
【面试必刷101】链表
MATLAB【函数求导】
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
Tita OKR: a dashboard to master the big picture
Intelligent water and fertilizer integrated control system
Foundation: 3 Opencv getting started images and videos
NFT监管要点和海外政策
Glitch free clock switching technology
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
Agrometeorological environment monitoring system