当前位置:网站首页>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开发的一款图形界面操作系统,只能运行与苹果提供的硬件之上。
边栏推荐
- 【面试必刷101】链表
- Huawei machine test questions column subscription Guide
- AVL树的理解和实现
- Introduction to R language
- 機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
- Foundation: 3 Opencv getting started images and videos
- Leetcode t39: combined sum
- Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
- 内存大小端
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
猜你喜欢

Screenshot tips

《单片机原理及应用》—定时器、串行通信和中断系统

Internet of things technology is widely used to promote intelligent water automation management

《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断

Nacos - 服务发现

一文纵览主流 NFT 市场平台版税、服务费设计

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR

Principle and application of single chip microcomputer - principle of parallel IO port

"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis

Bimianhongfu queren()
随机推荐
View drawing process analysis
Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
【MFC开发(16)】树形控件Tree Control
目标检测的yolov3、4、5、6总结
[Yu Yue education] Shandong Vocational College talking about railway reference materials
What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way
機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
分享2022上半年我读过的7本书
Review of week 280 of leetcode
[JS reverse] MD5 encryption parameter cracking
Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
The use of word in graduation thesis
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
又到年中,固定资产管理该何去何从?
15Mo3 German standard steel plate 15Mo3 chemical composition 15Mo3 mechanical property analysis of Wuyang Steel Works
Foundation: 2 The essence of image
Intelligent constant pressure irrigation system