当前位置: 首页 > news >正文

Shell脚本case in esac分支语句应用

记录:434

场景:Shell脚本case in esac分支语句应用。

版本:CentOS Linux release 7.9.2009。

1.case in esac格式

格式:

case 值 in 
模式1)expression;;
模式2)expression;;
模式n)expression;;
esac

解析:case是开始语句,esac是结束语句,;;表示分支结束。in 模式n)表示匹配分支。

2.使用case in esac

2.1脚本

脚本名称:b2023052811.sh

脚本内容:

#!/bin/bashecho "请输入字符串日期(格式:yyyymmdd):"
read day_str# date命令获取星期索引号,0是星期日,1-6是星期一到星期六
week_index=`date -d ${day_str} +%w`case ${week_index} in 0)echo "日期: ${day_str},是星期日.";;1)echo "日期: ${day_str},是星期一.";;2)echo "日期: ${day_str},是星期二.";;3)echo "日期: ${day_str},是星期三.";;4)echo "日期: ${day_str},是星期四.";;5)echo "日期: ${day_str},是星期五.";;6)echo "日期: ${day_str},是星期六.";;*)echo "输入信息不正确.";;
esac

2.2执行与输出

执行命令:bash b2023052811.sh

执行结果:

[root@hadoop211 tutorial]# bash b2023052811.sh 
请输入字符串日期(格式:yyyymmdd):
20230527
日期: 20230527,是星期六.
[root@hadoop211 tutorial]# bash b2023052811.sh 
请输入字符串日期(格式:yyyymmdd):
20230528
日期: 20230528,是星期日.

3.使用case in esac

3.1脚本

脚本名称:b2023052812.sh

脚本内容:

#!/bin/bashecho "请输入城市名称:"
read city_name
case ${city_name} in "杭州"|"宁波"|"绍兴")echo "${city_name},是浙江省的城市.";;"苏州"|"无锡"|"常州")echo "${city_name},是江苏省的城市.";;*)echo "${city_name},其它信息,暂不支持.";;
esac

3.2执行与输出

执行命令:bash b2023052812.sh

执行结果:

[root@hadoop211 tutorial]# bash b2023052812.sh 
请输入城市名称:
宁波
宁波,是浙江省的城市.
[root@hadoop211 tutorial]# bash b2023052812.sh 
请输入城市名称:
苏州
苏州,是江苏省的城市.
[root@hadoop211 tutorial]# bash b2023052812.sh 
请输入城市名称:
上海
上海,其它信息,暂不支持.

4.使用case in esac和while组合

4.1脚本

脚本名称:b2023052813.sh

脚本内容:

#!/bin/bashwhile true
dostop_flag=falseecho '请根据偏好输入技术框架名称,(如需结束体验,请输入EOF)'read techcase ${tech} in Hadoop)echo "The Apache™ Hadoop® project develops open-source software for reliable, scalable, distributed computing.";;Spark)echo "pache Spark™ is a multi-language engine for executing data engineering, data science, and machine learning on single-node machines or clusters.";;Hive)echo "The Apache Hive is a distributed, fault-tolerant data warehouse system that enables analytics at a massive scale and facilitates reading, writing, and managing petabytes of data residing in distributed storage using SQL";;Flink)echo "Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams";;DolphinScheduler)echo 'Apache DolphinScheduler is the modern data workflow orchestration platform with powerful user interface, dedicated to solving complex task dependencies in the data pipeline and providing various types of jobs available `out of the box`.';;EOF)stop_flag=trueecho "接收到结束字符:${tech},结束本次体验.";;*)echo "暂不支持该项技术.";;esac#取字符串变量时,使用$取值再比对   if [[ ${stop_flag} = true ]];thenbreakfi
done

4.2执行与输出

执行命令:bash b2023052813.sh

执行结果:

[root@hadoop211 tutorial]# bash b2023052813.sh 
请根据偏好输入技术框架名称,(如需结束体验,请输入EOF)
Hadoop
The Apache™ Hadoop® project develops open-source software for reliable, scalable, distributed computing.
请根据偏好输入技术框架名称,(如需结束体验,请输入EOF)
C++
暂不支持该项技术.
请根据偏好输入技术框架名称,(如需结束体验,请输入EOF)
EOF
接收到结束字符:EOF,结束本次体验.

以上,感谢。

2023年5月28日

http://www.lryc.cn/news/90141.html

相关文章:

  • 【线性dp必学四道题】线性dp四道经典例题【最长上升子序列】、【最长公共子序列】、【最长公共上升子序列(maxv的由来)】【最长公共子串】
  • 追寻幸福:探索幸福的关键特征和行为
  • Redis-02-集群
  • 【2023 · CANN训练营第一季】MindSpore模型快速调优攻略 第三章——MindSpore云上调试调优
  • python笔记17_实例演练_二手车折旧分析p2
  • android 12.0长按Power弹出关机对话框去掉屏幕截图和紧急呼救功能
  • 2023年下半年软考高级需要报班吗?
  • 使用WordPress提高企业敏捷性
  • SSM编程---Day 07
  • Seata术语
  • 【Axure教程】通过文本框维护下拉列表选项
  • 【C++】基础知识--输入/输出(5)
  • 经典文献阅读之--PIBT(基于可见树的实时规划方案)
  • SAP-MM-计算方案字段解析
  • go-gf框架两个表以事务方式写入示例
  • 2023-5-31第三十一天
  • 什么是MQTT?mqtt协议和http协议区别
  • 平台使用篇 | 批处理(bat)脚本使用教程(四)
  • 接口的讲解
  • G0第21章 :gin框架介绍、RESTful API、Gin渲染
  • python list,dict操作
  • 我有一个页面a,在页面a中调用了一个组件,然后组件中要切换页面a的一块区域,该怎么实现?
  • ChatGPT唤醒AI游戏:AIGC持续走深,游戏或成AI最佳抓手
  • 远程服务和web服务和前端,三方通过socket和websocket进行双向通信传输数据
  • Linux 网络基础(2)应用层(http/https协议、请求格式、响应格式、session、cookie、加密传输)
  • 解决sshfs挂载报错
  • 由于过多的连接错误而被 MySQL服务器 阻止
  • Go语言实现JDBC
  • ubuntu修改环境变量的几种方法
  • 基于html+css的图展示95