一、case条件语句

1.语法

case "变量" in
值 1)
指令 1...
;;
值 2)
指令 2...
;;
*)
指令 3...
esac

case条件语句的执行流程逻辑图

2.实践

(1)根据用户输入判断输入的是哪个数字

[root@codis-178 ~]# cat 9_1.sh
#!/bin/bash
read -p "Please input a number:" ans
case "$ans" in
1)
echo "The num you input is 1"
;;
2)
echo "The num you input is 2"
;;
[3-9])
echo "The num you input is $ans"
;;
*)
echo "Please input [0-9] int"
exit;
esac [root@codis-178 ~]# sh 9_1.sh
Please input a number:2
The num you input is 2
[root@codis-178 ~]# sh 9_1.sh
Please input a number:5
The num you input is 5
[root@codis-178 ~]# sh 9_1.sh
Please input a number:10
Please input [0-9] int

(2)打印一个水果菜单

1)apple

2)pear

3)banana

4)cherry

当用户输入对应的数字选择水果时,告诉他选择的水果是什么,并给水果单词加上一种颜色。

[root@codis-178 ~]# cat 9_2.sh
#!/bin/bash
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo '
==============
1.apple
2.pear
3.banana
4.cherry
============
'
read -p "pls select a num:" num
case "$num" in
1)
echo -e "${RED_COLOR}apple${RES}"
;;
2)
echo -e "${GREEN_COLOR}pear${RES}"
;;
3)
echo -e "${YELLOW_COLOR}banana${RES}"
;;
4)
echo -e "${BLUE_COLOR}cherry${RES}"
;;
*)
echo "muse be {1|2|3|4}"
esac [root@codis-178 ~]# sh 9_2.sh
9_2.sh: line 6: E[0m: command not found ==============
1.apple
2.pear
3.banana
4.cherry
============ pls select a num:1
apple
[root@codis-178 ~]# sh 9_2.sh
9_2.sh: line 6: E[0m: command not found ==============
1.apple
2.pear
3.banana
4.cherry
============ pls select a num:2
pear
[root@codis-178 ~]# sh 9_2.sh
9_2.sh: line 6: E[0m: command not found ==============
1.apple
2.pear
3.banana
4.cherry
============ pls select a num:3
banana

说明:

1)\E可以使用\033替代

2)[1的数字1表示加粗

3)31m表示红色

4)[0m表示关闭所有属性([1m表示高亮度,[4m表示下划线,[5m表示闪烁)

(3)给指定内容加指定的颜色

[root@codis-178 ~]# cat 9_3.sh
#!/bin/bash
plus_color(){
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
PINK_COLOR='\E[1;35m'
RES='\E[0m' if [ $# -ne 2 ];then
echo "Usage:$0 content {red|yellow|blue|green|pink}"
exit
fi case "$2" in
red|RED)
echo -e "${RED_COLOR} $1 ${RES}"
;;
yellow|YELLOW)
echo -e "${YELLOW_COLOR} $1 ${RES}"
;;
green|GREEN)
echo -e "${GREEN_COLOR} $1 ${RES}"
;;
blue|BLUE)
echo -e "${BLUE_COLOR} $1 ${RES}"
;;
pink|PINK)
echo -e "${PINK_COLOR} $1 ${RES}"
;;
*)
echo "Usage:$0 content {red|yellow|blue|green|pink}"
exit
esac
}
plus_color "I" red
plus_color "am" GREEN
plus_color "oldboy" blue
[root@codis-178 ~]# sh 9_3.sh
I
am
oldboy

3.企业应用

实现通过传参的方式往/etc/openvpn_authfile.conf里添加用户

传参要求:

参数-add,表示添加后面接的用户名

参数-del,表示删除后面接的用户名

参数-search,表示查找后面接的用户名

注:如果有同名用户,则不能添加,如果没有对应的用户,则无需删除,查找到用户或没有用户时应给出明确提示

[root@codis-178 ~]# cat 9_4.sh
#!/bin/bash
#Source function library
. /etc/init.d/functions #config file path
FILE_PATH=/etc/openvpn_authfile.conf
[ ! -f $FILE_PATH ] && touch $FILE_PATH
usage(){
cat <<EOF
USAGE: `basename $0` {-add|-del|-search} username
EOF
} #judge arg numbers
if [ $# -ne 2 ];then
usage
exit 2
fi case "$1" in
-a|-add)
shift
if grep "^$1$" ${FILE_PATH} >/dev/null 2>&1
then
action $"vpnuser,$1 is exis" /bin/false
exit
else
chattr -i ${FILE_PATH}
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
echo "$1" >> ${FILE_PATH}
[ $? -eq 0 ] && action $"Add $1" /bin/true
chattr +i ${FILE_PATH}
fi
;;
-d|-del)
shift
if [ `grep "\b$1\b" ${FILE_PATH}|wc -l` -lt 1 ]
then
action $"vpnuser,$1 is exis" /bin/false
exit
else
chattr -i ${FILE_PATH}
/bin/cp ${FILE_PATH} ${FILE_PATH}.$(date +%F%T)
sed -i "/^${1}$/d" ${FILE_PATH}
[ $? -eq 0 ] && action $"Del $1" /bin/true
chattr +i ${FILE_PATH}
exit
fi
;;
-s|-search)
shift
if [ `grep -w "$1" ${FILE_PATH}|wc -l` -lt 1 ]
then
echo $"vpnuser,$1 is not exist.";exit
else
echo $"vpnuser,$1 is exist.";exit
fi
;;
*)
usage
exit
;;
esac [root@codis-178 ~]# sh 9_4.sh
USAGE: 9_4.sh {-add|-del|-search} username
[root@codis-178 ~]# sh 9_4.sh -a oldboy
Add oldboy [ OK ]
[root@codis-178 ~]# rm -f /etc/openvpn_authfile.conf
rm: cannot remove `/etc/openvpn_authfile.conf': Operation not permitted
[root@codis-178 ~]# sh 9_4.sh -search oldboy
vpnuser,oldboy is exist.
[root@codis-178 ~]# sh 9_4.sh -search oldgirl
vpnuser,oldgirl is not exist.
[root@codis-178 ~]# sh 9_4.sh -add oldgirl
Add oldgirl [ OK ]
[root@codis-178 ~]# sh 9_4.sh -s oldgirl
vpnuser,oldgirl is exist.
[root@codis-178 ~]# sh 9_4.sh -d oldgirl
Del oldgirl [ OK ]
[root@codis-178 ~]# sh 9_4.sh -s oldgirl
vpnuser,oldgirl is not exist.

说明;

1)请注意grep精准过滤的三种用法

2)shift,将$1清除,将$2替换为$1,位置参数左移(这是为了取用户名)

3)chattr 加解锁文件

开发MySQL多实例中3306实例的启动停止脚本

[root@codis-178 ~]# cat 3306.sh
#!/bin/bash
#init
port=3306
mysql_user="root"
mysql_pwd="oldboy123"
CmdPath="/usr/local/mysql/bin"
mysql_sock="/data/${port}/mysql.sock" #startup function
function_start_mysql(){
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --default-file=/data/${port}/my.cnf 2>&1 >/dev/null &
else
printf "MySQL is running...\n"
exit
fi
} #stop function
function_stop_mysql(){
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
} #restart function
function_restart_mnysql(){
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 5
function_start_mysql
} case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mnysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac

4.case语句的适用性

(1)适合变量值较少且为固定的数字或字符串集合的情况

(2)写服务的启动脚本,传参不同且具有少量的字符串

Shell编程之case条件的更多相关文章

  1. shell编程之case分支语句

    shell编程之case分支语句 case分支语句和if的多分支语句很相似. if多分支语句一般用在有(区间范围)的地方 :例如:0-100之间. if需要判断多个不同的条件. case的分支语句用在 ...

  2. 03 shell编程之case语句与函数

    本文所有内容均来自当年博主当年学习笔记,若有不足欢迎指正 Shell编程之case语句与函数 学习目标: 掌握case语句编程 掌握shell函数的使用 目录结构: Case语句 Case语句的作用 ...

  3. SHELL编程之case与函数

    一.case语句概述 使用case语句改写if多分支可以使脚本结构更加清晰.层次分明 针对变量不同取值,执行不同的命令序列 case语句结构如下: case 变量值  in 模式1) 命令序列1 ;; ...

  4. Shell编程之IF条件

    一.if条件语句的知识与实践 1.if条件语句语法(单分支结构) 第一种: if < 条件表达式 > then 指令 fi 第二种: if < 条件表达式 >; then 指令 ...

  5. shell编程之awk命令详解

    shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...

  6. shell编程之if语句

    shell编程之if判断 目录 shell编程之if判断 1.整数比较 2.字符串比较 3.举例 1.数字比较 2.字符串比较 4.Other 1.整数比较 -eq 等于,如:if [ "$ ...

  7. shell 编程之 if...else case...esac

    shell的条件判断语句有三种 if...fi  语句 if...else...fi  语句 if...elif...fi  语句 例子: a=10; b=20; if [ $a -gt %b ] t ...

  8. shell 编程之 for while until 循环

    shell 的for循环 的格式如下: for 变量  in 列表 do ... done 列表是一组值的序列 每个值通过空格隔开 每循环一次,列表中的下一个值赋给变量 in 列表是可选的,如果不用他 ...

  9. shell编程学习笔记(九):Shell中的case条件判断

    除了可以使用if条件判断,还可以使用case 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script08.sh 开始编写scrip ...

随机推荐

  1. oracle怎么卸载

    Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.到目前仍在数据库市场上占有主要份额.劳伦斯·埃里森和他的朋友,之前的同事Bob ...

  2. CentOS 5 上安装Oracle11g

    原创作品,出自 "深蓝的blog" 博客.欢迎转载.转载时请务必注明下面出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  3. CentOS 6.5 安装Gitlab 7.12.2

    官网环境要求 参见:https://github.com/gitlabhq/gitlabhq GitLab is a Ruby on Rails application that runs on th ...

  4. billboard Shader待研究下

    Shader "Tut/Project/Billboard_1" { Properties { _MainTex ("Base (RGB)", 2D) = &q ...

  5. c/c++输入处理,制定变量参数和值

    void usage(char* s){ fprintf(stderr, "\n"); fprintf(stderr, "%s -s <source file> ...

  6. supervisor 与 yii定时任务

    https://www.jianshu.com/p/9abffc905645 https://www.cnblogs.com/ajianbeyourself/p/5534737.html https: ...

  7. jmeter通过json extrcator或者正则表达式获取json返回信息

    1.下载地址,及插件文档资料 https://jmeter-plugins.org/wiki/JSONPathExtractor/ json信息如下 { "error_code": ...

  8. JQUERY实现的小巧简洁的无限级树形菜单

    JQUERY实现的小巧简洁的无限级树形菜单,可用于后台或前台侧栏菜单!兼容性也比较好. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tra ...

  9. [转]JavaWeb之 Servlet执行过程 与 生命周期

    https://www.cnblogs.com/vmax-tam/p/4122105.html Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类 ...

  10. NoSQL-MongoDB with python

    前言: MongoDB,文档存储型数据库(document store).NoSQL数据库中,它独占鳌头,碾压其他的NoSQL数据库. 使用C++开发的,性能仅次C.与redis一样,开源.高扩展.高 ...