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 ...
随机推荐
- tomcat的bin目录中startup.bat/tomcat.6.exe/tomcat6w.exe区别
一.tomcat6.exe 与 startup.bat的区别 1.两者都可以用于启动Tomcat tomcat6.exe则是必须将tomcat注册Windows服务之后才可以用于启动tomcat服务; ...
- php 常面试
问题:请用最简单的语言告诉我PHP是什么? 回答:PHP全称:Hypertext Preprocessor,是一种用来开发动态网站的服务器脚本语言. 问题:什么是MVC? 回答:MVC由Model(模 ...
- 大量数据导入导致mysql自动重启
昨天晚上第十款做数据库迁移,数据库版本:Version: '5.1.61',数据量27G左右 message报错信息如下: Mar :: VM_163_210_tlinux kernel: [, oo ...
- asp.net网站底部的版权信息实现代码且可维护
网站底部的版权信息在特殊情况还是比较重要的所以在实现的时候一定要尽可能的做到可维护性,接下来将介绍一些技巧可达到可维护效果,感兴趣的你可不要错过了哈 一个大网站页面很多,如果每个版权信息直接写在下面, ...
- (转载)Unity3D研究院之使用 C#合成解析XML与JSON(四十一)
XML与JSON在开发中非常重要, 其实核心就是处理字符串.一个是XML的字符串一个是JSON的字符串,尤其是在处理网络请求的时候,肯定是要用的.另外现在JSON非常的流行,我写了一个简单的例子融合了 ...
- Python_selenium之执行JavaScript
Python_selenium之执行JavaScript 一.简略的介绍selenium执行JavaScript 1. Example 1进入浏览器之后,弹出一个alert弹框 #coding:utf ...
- cocos2d-x:七彩连珠
大学前,我一直以为学计算机就是学做游戏,或者玩游戏...(我果断的用大学的差不多五分之二实践了后面半块.) 大学后,人们一直以为学计算机的就是 修电脑.装机.盗qq号.word.excel....最近 ...
- iOS 数组的去重(普通的无序的去重和排序好的去重)
本文转载至 http://blog.csdn.net/zhaopenghhhhhh/article/details/24972645 有时需要将NSArray中去除重复的元素,而存在NSArray中的 ...
- A Simple Problem with Integers(线段树)
F - A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:% ...
- Python3.6全栈开发实例[005]
5.接收两个数字参数,返回比较大的那个数字. def compare(a,b): return a if a > b else b # 三元表达式 print(compare(20,100))