当前位置:网站首页>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开发的一款图形界面操作系统,只能运行与苹果提供的硬件之上。
边栏推荐
- factory type_id::create过程解析
- 1. Connection between Jetson and camera
- 2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
- I would like to know the process of stock registration and account opening by mobile phone? In addition, is it safe to open a mobile account?
- In depth learning training sample amplification and tag name modification
- factory type_ Id:: create process resolution
- MAVROS发送自定义话题消息给PX4
- Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
- 电脑小技巧
- What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
猜你喜欢
随机推荐
深度学习训练样本扩增同时修改标签名称
【MFC开发(17)】高级列表控件List Control
截图小妙招
R语言入门
The meaning of yolov5 training visualization index
Nacos - 配置管理
factory type_id::create过程解析
MySQL8.0学习记录17 -Create Table
MAVROS发送自定义话题消息给PX4
How can enterprises and developers take the lead in the outbreak of cloud native landing?
機動目標跟踪——當前統計模型(CS模型)擴展卡爾曼濾波/無迹卡爾曼濾波 matlab實現
Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
Foundation: 3 Opencv getting started images and videos
【MFC开发(16)】树形控件Tree Control
Redis publish subscription
【面试必刷101】链表
个人装修笔记
Agrometeorological environment monitoring system
Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
Advanced level of C language pointer (Part 1)





![Matlab [functions and images]](/img/8a/d2f68b5a7ed396ad20234c0aa24953.jpg)


