shell脚本中的case条件语句介绍和使用案例

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

shell脚本中的case条件语句介绍和使⽤案例
#前⾔:这篇我们接着写shell的另外⼀个条件语句case,上篇讲解了if条件语句。

case条件语句我们常⽤于实现系统服务启动脚本等场
景,case条件语句也相当于if条件语句多分⽀结构,多个选择,case看起来更规范和易读
#case条件语句的语法格式
case"变量"in
值1)
指令1...
;;
值2)
指令2...
;;
*)
指令3...
esac
#说明:当变量的值等于1时,那么就会相应的执⾏指令1的相关命令输出,值等于2时就执⾏指令2的命令,以此类推,如果都不符合的话,则执⾏*后⾯的指令,要注意内容的缩进距离
#简单记忆
case"找⼯作条件"in
给的钱多)
给你⼯作...
;;
给股份)
给你⼯作...
;;
有发展前景)
可以试试...
;;
*)
bye bye !!
esac
#实践使⽤
实践1.根据⽤户的输⼊判断⽤户输⼊的是哪个数字,执⾏相应动作
#如果⽤户输⼊的是1-9的任意⼀个数字,则输出对应输⼊的数字,如果是别的字符,则提⽰输出不正确并退出程序
[root@shell scripts]# cat num.sh
#!/bin/bash
#create by guoke
#function number input
read -p "please input a number:" num #打印信息提⽰⽤户输⼊,输⼊信息赋值给num变量
case"$num"in
1)
echo "The num you input is 1"
;;
[2-5])
echo "The num you input is 2-5"
;;
[6-9])
echo "The num you input is 6-9"
;;
*)
echo "please input number[1-9] int"
exit;
esac
#说明:使⽤read读取⽤户输⼊的数据,然后使⽤case条件语句进⾏判断,根据⽤户输⼊的值执⾏相关的操作
#执⾏效果
[root@shell scripts]# sh num.sh
please input a number:1
The num you input is1
[root@shell scripts]# sh num.sh
please input a number:3
The num you input is2-5
[root@shell scripts]# sh num.sh
please input a number:4
The num you input is2-5
[root@shell scripts]# sh num.sh
please input a number:8
The num you input is6-9
[root@shell scripts]# sh num.sh
please input a number:a
please input number[1-9] int
实践2.打印⼀个如下的⽔果菜单
(1) banana
(2) apple
(3) orange
(4) cherry
#脚本编写
[root@shell scripts]# cat menu.sh
#!/bin/bash
#create by guoke
#function print menu
RED_COLOR='\E[1;31m'
GREEN_COLOR='\E[1;32m'
YELLOW_COLOR='\E[1;33m'
BLUE_COLOR='\E[1;34m'
RES='\E[0m'
echo ' #使⽤echo打印菜单
#############################
1.banana
2.apple
3.pear
4.cherry
#############################
'
read -p "please select a num:" num
case"$num"in
1)
echo -e "${YELLOW_COLOR} banana ${RES}"
;;
2)
echo -e "${RED_COLOR} apple ${RES}"
;;
3)
echo -e "${GREEN_COLOR} pear ${RES}"
;;
4)
echo -e "${BLUE_COLOR} cherry ${RES}"
;;
*)
echo "please input {1|2|3|4}"
esac
#说明:定义颜⾊,使⽤read读取⽤户输⼊的数据,然后使⽤case条件语句进⾏判断,根据⽤户输⼊的值执⾏相关的操作,给⽤户输⼊的⽔果添加颜⾊
#扩展:输出菜单的另外种⽅式
cat<<-EOF
===============================
1.banana
2.apple
3.pear
===============================
EOF
#执⾏效果
#如果输⼊不正确或者不输⼊的话就打印帮助
[root@shell scripts]# sh menu.sh
#############################
1.banana
2.apple
3.pear
4.cherry
#############################
please select a num:
please input {1|2|3|4}
#输⼊选项中的数字,打印相关信息
实践3.开发nginx启动脚本
#主要思路:
#1.主要通过判断nginx的pid⽂件有⽆存在,通过返回值查看有没有运⾏
#2.通过case语句获取参数进⾏判断
#3.引⼊系统函数库functions中的action函数
#4.对函数及命令运⾏的返回值进⾏处理
#5.设置开机⾃启动
#附上nginx编译安装过程
#!/bin/bash
yum install gcc pcre pcre-devel wget openssl openssl-devel.x86_64 -y
mkdir -p /home/demo/tools
cd /home/demo/tools/
wget -q /download/nginx-1.6.3.tar.gz
useradd nginx -s /sbin/nologin -M
tar xf nginx-1.6.3.tar.gz
cd nginx-1.6.3/
./configure --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module --with-http_ssl_module make
ln -s /application/nginx-1.6.3 /application/nginx/ #做软连接
/application/nginx/sbin/nginx -t #检查语法
/application/nginx/sbin/nginx #启动服务
#脚本编写
[root@shell init.d]# chmod +x /etc/init.d/nginxd
[root@shell init.d]# cat nginxd
#!/bin/bash
#chkconfig: 2345 40 98 #设定2345级别,开机第40位启动脚本,关机第98位关闭脚本#create by guoke
#email:1075792988@
#function nginx start scripts
[ -f /etc/init.d/functions ] && source /etc/init.d/functions #引⼊系统函数库
PIDFILE=/application/nginx/logs/nginx.pid #定义PID⽂件路径
NGINX=/application/nginx/sbin/nginx #定义启动命令路径
value(){ #定义返回值函数
RETVAL=$?
if [ $RETVAL -eq 0 ];then
action "Nginx is $1" /bin/true
else
action "Nginx is $1" /bin/true
fi
}
start(){ #定义启动函数
if [ -f $PIDFILE ];then #判断PIDFILE存不存在,存在就打印运⾏,否则就启动
echo "Nginx is running"
else
$NGINX
value start #调⽤返回值函数
fi
}
stop(){ #定义停⽌函数
if [ ! -f $PIDFILE ];then #也是通过判断PID⽂件是否存在然后进⾏相关操作
echo "Nginx not running"
else
$NGINX -s stop
value stop
fi
}
reload(){ #定义重启函数
if [ ! -f $PIDFILE ];then
echo "not open $PIDFILE no such directory"
else
$nginx -s reload
value reload
fi
}
case"$1"in #使⽤case接收脚本传参的字符串
start) #如果第⼀个参数为start,调⽤start函数
start
;;
stop) #如果第⼀个参数为stop,调⽤stop函数
stop
;;
reload)
stop
sleep 1
start
;;
*)
echo "USAGE:$0 {stop|start|reload}"
exit 1
esac
#执⾏效果
[root@shell init.d]# sh nginx stop
Nginx is stop [ OK ]
[root@shell init.d]# sh nginx start
Nginx is start [ OK ]
[root@shell init.d]# sh nginx reload
Nginx is stop [ OK ]
Nginx is start [ OK ]
实践4.开发跳板机
#要求⽤户登录到跳板机后只能执⾏管理员给定的选项动作,不能中断脚本⽽到跳板机服务器上执⾏任何系统命令#思路
1.⾸先做好ssh key验证登录
2.实现远程连接菜单选择脚本
3.利⽤Linux信号防⽌⽤户在跳板机上操作
4.⽤户登录后就调⽤脚本
#操作过程
3.1.做ssh免密钥登录,发送到各个主机,如果机器多的话可以使⽤脚本进⾏循环发送
[demo@shell ~]$ ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/demo/.ssh/id_dsa):
Created directory '/home/demo/.ssh'.
Your identification has been saved in /home/demo/.ssh/id_dsa.
Your public key has been saved in /home/demo/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:BTFfcC2hMKBzuZeUYylC3qgza7z4X6j3RBlwq8Beoak demo@shell
The key's randomart image is:
+---[DSA 1024]----+
| + o.*...+o |
| . = B o O +. . |
| = B B * + . |
| o + = B + |
|E = . + S |
| . + o . |
| + . o |
| o o.o |
|..+o... |
+----[SHA256]-----+
#命令说明:⼀键⽣成密钥,不⽤按回车。

-t:指定要创建的密钥类型,-P:提供旧密码,空表⽰不需要密码,-f:指定位置
#将公钥拷贝到其他服务器的demo⽤户
[demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub "demo@192.168.86.129"
[demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub "demo@192.168.86.130"
[demo@shell ~]$ ssh-copy-id -i .ssh/id_dsa.pub "demo@192.168.86.131"
#3.2.编写脚本
[root@shell scripts]# cat tiaobanji.sh
#!/bin/bash
trapper(){ #定义屏蔽信号函数
trap '' INT QUIT TSTP TERM HUB
}
menu(){ #定义菜单列表函数
cat<<-EOF #加-后⾯的EOF就可以不⽤顶格
==============Host List==============
1) 192.168.86.129
2) 192.168.86.130
3) 192.168.86.131
4) 192.168.86.132
5) exit
=====================================
EOF
}
USER=demo
host(){ #定义主机列表函数
case"$1"in
1)
ssh $USER@192.168.86.129
;;
2)
ssh $USER@192.168.86.130
;;
3)
ssh $USER@192.168.86.131
;;
4)
ssh $USER@192.168.86.132
;;
5)
exit
esac
}
main(){ #定义主函数
while : #while循环,⼀直循环
do
trapper #调⽤trapper函数
clear #清屏
menu #调⽤菜单函数
read -p "please select a num:" num #获取⽤户输⼊
host $num #调⽤主机列表函数和传⼊的参数,进⾏远程登录
done
}
main #调⽤主函数
#3.3.编写脚本进⾏判断,判断是否是root⽤户登录,如果不是root⽤户就执⾏脚本,弹出跳板机界⾯
[root@shell ~]# cd /etc/profile.d/
[root@shell profile.d]# cat jump.sh
#!/bin/bash
[ $UID -ne 0 ] && . /scripts/tiaobanji.sh
#3.4.测试
#登录demo普通⽤户输⼊密码的时候就会直接跳到选项卡页⾯了
#选项卡页⾯
==============Host List==============
1) 192.168.86.129
2) 192.168.86.130
3) 192.168.86.131
4) 192.168.86.132
5) exit
=====================================
please select a num:1 #进⾏选择
Last login: Tue Mar 3123:48:332020from192.168.86.128
[demo@mysql ~]$
#3.5.提⽰:跳板机的安全
1.禁⽌跳板机可以从外⽹IP进⾏登录,只能从内⽹IP登录
2.其他服务器也限制只能内⽹IP登录,同时禁⽌root登录,做完ssh key认证,将密码登录禁⽌,通过免密码登录到其他服务器
#总结:if条件语句主要⽤于取值判断、⽐较,应⽤⽐较⼴,case条件语句主要是写服务的启动脚本,各有各的优势。

好了,shell脚本的条件语句就讲解到这⾥了,接下来会继续写shell脚本的循环(包括for,while等),如果写的不好的地⽅还望指出,多多交流提⾼,下次再会。

相关文档
最新文档