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)写服务的启动脚本,传参不同且具有少量的字符串

posted @ 2017-08-30 18:03  BXBZ—边学边做  阅读(346)  评论(0编辑  收藏  举报