当前位置:网站首页>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开发的一款图形界面操作系统,只能运行与苹果提供的硬件之上。
边栏推荐
- Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
- DID的使用指南,原理
- 《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
- SPL Introduction (I)
- VSYNC+三重缓存机制+Choreographer
- 明明设计的是高带宽,差点加工成开路?
- Guidelines and principles of did
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- Huawei machine test questions column subscription Guide
- Only in China! Alicloud container service enters the Forrester leader quadrant
猜你喜欢

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates

Intelligent constant pressure irrigation system

Memory size end

个人装修笔记

明明设计的是高带宽,差点加工成开路?

DID的使用指南,原理

It is designed with high bandwidth, which is almost processed into an open circuit?

【无标题】

MATLAB【函数求导】

《微机原理》-绪论
随机推荐
Leetcode t34: find the first and last positions of elements in a sorted array
Advanced C language pointer (Part 2)
C语言学生信息管理系统
内存大小端
Matlab [function derivation]
Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
In depth learning training sample amplification and tag name modification
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
电脑小技巧
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
Configuration and startup of Chang'an chain synchronization node
MATLAB【函数和图像】
5mo3 UHI HII HII 17mn4 19Mn6 executive standard
目标检测的yolov3、4、5、6总结
SPL installation and basic use (II)
C语言指针的进阶(上篇)
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
Tita OKR: a dashboard to master the big picture
Audio audiorecord create (I)
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度