设为首页 收藏本站
查看: 819|回复: 0

shell脚本编程学习之路-case语句

[复制链接]

尚未签到

发表于 2018-8-20 09:54:52 | 显示全部楼层 |阅读模式
1.case结构条件语句语法
  case语句实际上就是规范的多分支if语句
  case “字符串变量”in
  值1)指令1…
  ;;
  值2)指令2…
  ;;
  *)指令3…
  esac
  中文编程语法:
  case “找女朋友条件”in
  有房)嫁给你…
  ;;
  你爸是李刚)嫁给你…
  ;;
  努力吃苦)可以考虑先谈朋友…
  ;;
  *)good bye!!!
  esac

2.简单case脚本
  输入1、2、3分别输出对应的值
  

[root@shellbiancheng jiaobenlianxi]# cat case01.sh  
#!/bin/bash
  
usage() {
  
echo "USAGE:$0 {1|2|3}" contents
  
exit 1
  
}
  

  
num() {
  
case "$1" in
  
1)echo "1"
  
;;
  
2)echo "2"
  
;;
  
3)echo "3"
  
;;
  
*)usage
  
esac
  
}
  

  
main() {
  if [ $# -ne 1 ];then
  usage
  fi
  num $1
  
}
  

  
main $*
  

3.执行脚本打印一个水果菜单如下:
  a.apple
  b.pear
  c.banana
  d.cherry
  当用户选择水果的时候,打印告诉它选择的水果是什么并给选择的水果加上颜色。要求case语句实现。
  

[root@shellbiancheng jiaobenlianxi]# cat menufruit.sh  
#!/bin/bash
  
RED_COLOR='\E[1;31m'
  
GREEN_COLOR='\E[1;32m'
  
YELLOW_COLOR='\E[1;33m'
  
BLUE_COLOR='\E[1;34m'
  
PINK='\E[1;35m'
  
SHAN='\E[31;5m'  提示闪烁功能结合 echo –e 使用
  
RES='\E[0m'
  
menu(){
  
cat  ../init.d/rpcgssd
  
[root@shellbiancheng logs]# ll /etc/rc.d/rc3.d/ |grep S20
  

  脚本代码如下:
  

[root@shellbiancheng jiaobenlianxi]# cat nginx.sh  
#!/bin/bash
  
# chkconfig: 2345 20 16
  
# description: nginx is a http server
  
#Date: 2018-04-07
  
#Author: Create by linzhongniao
  
#Mail: xxxxxxxxxx@163.com
  
#Function:This scripts function is Nginx startup script.
  
#Version: 1.1
  

  
if [ -f /etc/init.d/functions ];then
  . /etc/init.d/functions
  
fi
  
pidfile=/usr/local/nginx/logs/nginx.pid
  
SHAN='\E[31;5m'
  
RES='\E[0m'
  
nginx=/usr/local/nginx/sbin/nginx
  
RETVAL=0
  
linzhongniao() {
  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
  echo -e ${SHAN}"nginx is running"${RES}
  else
  $nginx
  linzhongniao started
  fi
  return $RETVAL
  
}
  
stop() {
  if [ ! -f $pidfile ];then
  echo -e  ${SHAN}"nginx is stopped"${RES}
  else
  $nginx -s stop
  linzhongniao stopped
  fi
  return $RETVAL
  
}
  

  
restart() {
  printf "Restarting Nginx ...\n"
  stop
  sleep 2
  start
  
}
  

  
reload() {
  if [ ! -f $pidfile ];then
  echo -e ${SHAN}"Can't open $pidfile,no such file or directory"${RES}
  else

  $nginx -s>
  linzhongniao>  fi
  return $RETVAL
  
}
  

  
usage() {
  echo -e ${SHAN}"USAGE:$0 {start|stop|restart|reload}"${RES}
  
}
  

  
main() {
  
case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  restart)
  restart
  ;;
  reload)
  reload
  ;;
  *)usage
  exit $RETVAL
  esac
  
}
  
main $1
  
exit $RETVAL
  

  最后我们把它加载到chkconfig里面,完成nginx服务开机自启动
  

[root@shellbiancheng init.d]# chkconfig nginx on  
[root@shellbiancheng init.d]# chkconfig --list nginx
  
nginx      0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
  
关闭开机自启动
  
[root@shellbiancheng init.d]# chkconfig nginx off
  
[root@shellbiancheng init.d]# chkconfig --list nginx
  
nginx      0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
  

5.case语句小结
  (1)case语句相当于多分支的if语句。case语句优势更规范、易读。
  (2)case语句适合变量值少,并且固定的数字或字符串的集合。(1,2,3)或(start,stop,restart)。
  (3)系统服务启动脚本传参多使用case语句,参考/etc/init.d/rsyslog的启动脚本。
  (4)所有case语句都可以使用if实现,但是case语句更规范清晰一些。
  (5)case语句一般适合于服务的启动脚本。
  (6)case的变量的值如果已知固定的start/restart/stop的元素比较适合。
  语句小结:
  (1)case主要是写启动脚本,范围较窄。
  (2)if取值判断、比较、应用广泛。

6.学习系统脚本
  多向系统脚本学习
  /etc/init.d/functions
  函数库functions详解:http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html
  /etc/rc.d/rc.sysinit
  /etc/init.d/rpcbind
  /etc/init.d/nfs
  /etc/init.d/httpd



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-554130-1-1.html 上篇帖子: shell脚本在企业中的使用案例(1)--一键式打包 下篇帖子: shell 文件重命名
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表