Loading

Shell 编程 case语句

CentOS-Logo

本篇主要写一些shell脚本case语句的使用。


字符判断

#!/bin/bash
read -p "请输入一个字符:" char
case $char in
[a-z]|[A-Z])
  echo "输入的是字母"
  ;;
[0-9])
  echo "输入的是数字"
  ;;
*)
  echo "输入的是特殊符号"
esac
[root@localhost ~]# vim char.sh
[root@localhost ~]# chmod +x char.sh 
[root@localhost ~]# ./char.sh 
请输入一个字符:a
输入的是字母
[root@localhost ~]# ./char.sh 
请输入一个字符:B
输入的是字母
[root@localhost ~]# ./char.sh 
请输入一个字符:1
输入的是数字
[root@localhost ~]# ./char.sh 
请输入一个字符:!
输入的是特殊符号

成绩判断

#!/bin/bash
read -p "请输入分数(0-100):" score
case $score in
[8-9][0-9]|100)
  echo "成绩优秀"
  ;;
7[0-9])
  echo "成绩良好"
  ;;
6[0-9])
  echo "成绩合格"
  ;;
[0-9]|[1-5][0-9])
  echo "成绩不合格"
  ;;
*)
  echo "输入错误"
esac
[root@localhost ~]# vim score.sh
[root@localhost ~]# chmod +x score.sh 
[root@localhost ~]# ./score.sh 
请输入分数(0-100):100
成绩优秀
[root@localhost ~]# ./score.sh 
请输入分数(0-100):88
成绩优秀
[root@localhost ~]# ./score.sh 
请输入分数(0-100):77
成绩良好
[root@localhost ~]# ./score.sh 
请输入分数(0-100):66
成绩合格
[root@localhost ~]# ./score.sh 
请输入分数(0-100):55 
成绩不合格
[root@localhost ~]# ./score.sh 
请输入分数(0-100):4
成绩不合格
[root@localhost ~]# ./score.sh 
请输入分数(0-100):333
输入错误
posted @ 2019-10-08 08:27  LinSenGeGe  阅读(601)  评论(0编辑  收藏  举报