博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
case语法
阅读量:7106 次
发布时间:2019-06-28

本文共 1715 字,大约阅读时间需要 5 分钟。

语法:

case "字符串变量" in

值1)指令

;;

值2)指令

;;

值*)指令

;;

esac

下面我们来作一个小脚本:

#!/bin/bash

read -p "please input one the number:" a

case "$a" in

1)

echo "you input the number is 1"

;;

2)

echo "you input the number is 2"

;;

[3-9])

echo "you input the number is $a"

;;

*)

echo "you input the number more than 10!"

;;

esac

下面是这个脚本的执行效果:

[root@zhouyu shell]# sh case.sh 

please input one the number:1

you input the number is 1

[root@zhouyu shell]# sh case.sh 

please input one the number:2

you input the number is 2

[root@zhouyu shell]# sh case.sh

please input one the number:3

you input the number is 3

[root@zhouyu shell]# sh case.sh

please input one the number:4

you input the number is 4

[root@zhouyu shell]# sh case.sh

please input one the number:10

you input the number more than 10!

[root@zhouyu shell]# 

如果我们用if语句去实现的话是这样的

#!/bin/bash

read -p "please input one the number:" a

if [ $a -eq 1 ];then

echo "you input the number is 1"

elif [ $a -eq 2 ];then

echo "you input the number is 2"

elif [ $a -ge 3 -a $a -le 9  ];then

echo "you input the number is $a"

else

echo "you input the number more than 10!"

fi

"case_if.sh" 11L, 283C 已写入                                     

[root@zhouyu shell]# sh case_if.sh  

please input one the number:1

you input the number is 1

[root@zhouyu shell]# sh case_if.sh 

please input one the number:2

you input the number is 2

[root@zhouyu shell]# sh case_if.sh

please input one the number:3

you input the number is 3

[root@zhouyu shell]# sh case_if.sh 

please input one the number:4

you input the number is 4

[root@zhouyu shell]# sh case_if.sh 

please input one the number:10

you input the number more than 10!

通过上面我们可以知道,其实用case的话比较快,因为它不用比较,其实if的功能case可以实现,只是有时候用case比较麻烦,所以就用if语句

本文转自 周子琪 51CTO博客,原文链接:http://blog.51cto.com/izhouyu/1891998

转载地址:http://qdjhl.baihongyu.com/

你可能感兴趣的文章
[转] xgboost
查看>>
[转载]什么是“成功的项目”:谈谈软件交付价值
查看>>
easyui datagrid 动态表头配置
查看>>
web安全之SQL注入---第五章 如何预防SQL注入 ?
查看>>
KSFramework:Unity3D开发框架快速入门
查看>>
Daikon Forge GUI 制作UI面板
查看>>
转载:百度百科经典算法集合
查看>>
4.2、Android Studio压缩你的代码和资源
查看>>
如何将简单CMS后台管理系统示例转换为Java、Php等不同后台语言的版本
查看>>
Ext JS添加子组件的误区
查看>>
【js jQuery】map集合 循环迭代取值---以及 map、json对象、list、array循环迭代的方法和区别...
查看>>
新浪微博Oauth2.0授权认证及SDK、API的使用(Android)
查看>>
juery解决获取浏览器可视区域的兼容性问题
查看>>
Android中如何使用JUnit进行单元测试 eclipse
查看>>
微信硬件设备接入接口协议
查看>>
html+js实现四则元算计算器
查看>>
冒泡排序
查看>>
命名空间与use
查看>>
用户登录体验之密码框设计
查看>>
Variational Inference for Crowdsourcing
查看>>