Shell编程之case条件
一、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条件的更多相关文章
- shell编程之case分支语句
shell编程之case分支语句 case分支语句和if的多分支语句很相似. if多分支语句一般用在有(区间范围)的地方 :例如:0-100之间. if需要判断多个不同的条件. case的分支语句用在 ...
- 03 shell编程之case语句与函数
本文所有内容均来自当年博主当年学习笔记,若有不足欢迎指正 Shell编程之case语句与函数 学习目标: 掌握case语句编程 掌握shell函数的使用 目录结构: Case语句 Case语句的作用 ...
- SHELL编程之case与函数
一.case语句概述 使用case语句改写if多分支可以使脚本结构更加清晰.层次分明 针对变量不同取值,执行不同的命令序列 case语句结构如下: case 变量值 in 模式1) 命令序列1 ;; ...
- Shell编程之IF条件
一.if条件语句的知识与实践 1.if条件语句语法(单分支结构) 第一种: if < 条件表达式 > then 指令 fi 第二种: if < 条件表达式 >; then 指令 ...
- shell编程之awk命令详解
shell编程之awk命令详解 a:focus { outline: thin dotted #333; outline: 5px auto -webkit-focus-ring-color; out ...
- shell编程之if语句
shell编程之if判断 目录 shell编程之if判断 1.整数比较 2.字符串比较 3.举例 1.数字比较 2.字符串比较 4.Other 1.整数比较 -eq 等于,如:if [ "$ ...
- shell 编程之 if...else case...esac
shell的条件判断语句有三种 if...fi 语句 if...else...fi 语句 if...elif...fi 语句 例子: a=10; b=20; if [ $a -gt %b ] t ...
- shell 编程之 for while until 循环
shell 的for循环 的格式如下: for 变量 in 列表 do ... done 列表是一组值的序列 每个值通过空格隔开 每循环一次,列表中的下一个值赋给变量 in 列表是可选的,如果不用他 ...
- shell编程学习笔记(九):Shell中的case条件判断
除了可以使用if条件判断,还可以使用case 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script08.sh 开始编写scrip ...
随机推荐
- YII安装步骤(windows)
一.首先你得下个YII框架的源码 :下载地址:http://www.yiiframework.com/download/ 二.把下载到的源码解压放到一个PHP可访问的位置:如我的 F:\site(已具 ...
- centos7 virtualbox使用internal network 内网模式
1)打开对应虚拟机的Settings,点开Network, 2)Adapter1如果已经选了挂到Bridged Adapter,则点开Adapter2, 3)选择挂到 Internal Network ...
- tinyint(4),tinyint(80)有什么区别
tinyint格式: TINYINT[(M)] [UNSIGNED] [ZEROFILL] M默认为4 Tinyint占用1字节的存储空间,即8位(bit). 带符号的范围是-128到127.无符号的 ...
- javascript拼接html代码
转自开源中国社区:http://www.oschina.net/code/snippet_94055_21640经常做jsp开发的朋友可能遇到一个情况,显示列表数据不是table,而是div或者其他很 ...
- iOS --转载2018苹果$299美元企业级开发者账号申请攻略
前言篇 现在苹果企业级开发者账号申请十分严格,大部分企业都无法申请下来,本人尝试过多次申请,现将一些审核技巧分享出来,希望能帮到你们通过申请,需要帮助请看本文最后 2018年6月7号申请成功案例 ...
- http://localhost:8080请求用户名和密码。信息为:“XDB” 解决办法
windows查看端口占用情况 cmd下 netstat -ano 查看端口和对应的服务 为2520 Oracle的服务 源博客: http://blog.163.com/jxguo_05/blog/ ...
- Python踩坑:类与类对象类型参数传递与使用
前言 对初学者来说,Python确实简单好用,毕竟动态类型语言,不用定义就可以拿来用,类型之间随意转换简直不要太方便,因此Python用来写写小脚本,爬虫程序什么的,没什么问题. 不过,一旦用来开发稍 ...
- node.js中的路由(url)初步
1.建立n4_root.js var http = require('http'); var url = require('url'); //这是node.js中自带的var router = req ...
- EasyDSS流媒体服务器软件(支持RTMP/HLS/HTTP-FLV/视频点播/视频直播)-正式环境安装部署攻略
EasyDSS流媒体服务器软件,提供一站式的转码.点播.直播.时移回放服务,极大地简化了开发和集成的工作. 其中,点播功能主要包含:上传.转码.分发.直播功能,主要包含:直播.录像, 直播支持RTMP ...
- 双向认证 HTTPS双向认证
[微信支付]微信小程序支付开发者文档 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3 HTTPS双向认证使用说明 ...