首页 > 其他分享 >第五章:流程控制之case语句

第五章:流程控制之case语句

时间:2022-09-20 17:13:03浏览次数:57  
标签:case 语句 nginx stop echo start 第五章 bin root

流程控制之case语句

一、语法

case 变·量 in
模式一)
	命令序列1
	;;
模式二)
	命令序列2
	;;
...
*)
	无匹配后命令序列
esac

二、案例

1)案例一

#判断用户的类型

[root@db04 /scripts/day05]# vim user.sh 
#!/bin/bash

read -p "please input user name:" name
[ -z "$name" ] && echo "The user name must exist" && exit

case $name in
"root")
    echo "admin"
    ;;
"jh")
    echo "com user"
    ;;
*)
    echo "other user"
esac

[root@db04 /scripts/day05]#  chmod +x user.sh

2)案例二

#编写nginx启动脚本

[root@db04 /scripts/day05]# vim nginx.sh 
#!/bin/bash


[ $# -ne 1 ] && echo "Usage: $0 {start|stop|restart|reload|status}" && exit

function start() {
    ps aux |grep [n]ginx |grep -q master
    if [ $? -eq 0 ];then
        echo "nignx is started"
    else
        /usr/local/nginx/sbin/nginx &>/dev/null
        if [ $? -eq 0 ];then
            echo "nginx start success..."
        else
            echo "nginx start failed"
        fi
    fi
}

function stop(){
   ps aux |grep [n]ginx |grep -q master
    if [ $? -eq 0];then
       /usr/local/nginx/sbin/nginx -s stop
       sleep 3
       ps aux |grep [n]ginx |grep -q  master      
       if [ $? -ne 0 ];then
           echo "nginx stop success..."
           sleep 3
       else
           echo "nginx stop failed"
       fi
    else
        echo "nginx is stopped"
    fi
}

case $1 in
"start")
    start
    ;;
"stop")
    stop
    ;;

"restart")
    stop
    start
    ;;

"reload")
    stop
    start
    ;;

"status")
    ps aux |grep [n]ginx |grep -q  master
    if [ $? -eq 0 ];then
        echo "nginx is up"
    else
        echo "ngins is down"
    fi
    ;;
*)
    echo "Usage: $0 {start|stop|restart|reload|status}"
esac

[root@db04 /scripts/day05]#  chmod +x nginx.sh

3)案列三

[root@jh shell]# vim nginx_stat.sh
#!/bin/bash
. /etc/init.d/functions
if [ $# -ne 1 ]
then
echo "USAGE $0 {start|stop|restart}"
exit 1
fi
if [ "$1" == "start" ]
then
action "start nginx" /bin/true
elif [ "$1" == "stop" ]
then
action "stop nginx" /bin/true
elif [ "$1" == "restart" ]
then
action "restart nginx" /bin/true
else
echo "USAGE $0 {start|stop|restart}"
exit 1
fi
[root@jh shell]# chmod +x nginx_stat.sh
[root@jh shell]# ./nginx_stat.sh start
start nginx 						[ 确定 ]
[root@jh shell]# ./nginx_stat.sh restart
restart nginx 						[ 确定 ]
[root@jh shell]# ./nginx_stat.sh
USAGE ./nginx_stat.sh {start|stop|restart}

4)案列四

# 储备知识1
netstat -lntup|grep  ":80\b"  # \b锚定单词的结尾
# 储备知识2
action:打印一段信息并执行给定的命令,然后根据给定命令的执行的结果来调用 success,failure方
法,确定最终显示的内容
[root@jh shell]# action "nginx start is" :
nginx start is                       [  确定 ]
[root@jh shell]# action "nginx start is" /bin/true
nginx start is                       [  确定 ]
[root@jh shell]# action "nginx start is" /bin/false
nginx start is                       [失败]


# 代码
[root@jh shell]# vim nginx_stat.sh
#!/bin/bash
. /etc/init.d/functions
args=$1
fun(){
[ $? -eq 0 ] && action "Nginx $args is " /bin/true  || echo "Nginx $args is
" /bin/false
}
case $1 in
 start)
   netstat -lntup|grep  ":80\b" &>/dev/null
   if [ $? -eq 0 ]
   then
     echo "Nginx is runing..."
   else
     /usr/sbin/nginx
     fun
   fi
   ;;
 stop)
   /usr/sbin/nginx -s stop
   fun
   ;;
 reload)
   /usr/sbin/nginx -s reload
   fun
   ;;
 restart)
   netstat -lntup|grep  ":80\b" &>/dev/null
   if [ $? -ne 0 ]
   then
     /usr/sbin/nginx
    [ $? -eq 0 ] && echo "Nginx start is ok" || echo "Nginx start is
failed"
   else
     /usr/sbin/nginx -s stop              
    [ $? -eq 0 ] && echo "Nginx stop is ok" || echo "Nginx stop is failed"
     sleep 2
     /usr/sbin/nginx
     fun
   fi
   ;;
 status)
   netstat -lntup|grep  ":80\b" &>/dev/null
   if [ $? -eq 0 ]
   then
     echo "Nginx is runing ..."
   else
     echo "Nginx is not runing ..."
   fi
   ;;
  *)
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit 2
esac

5)案例五

#编写一个简易跳板机脚本

# 储备知识
Linux中断信号区别为:键入不同、对应操作不同、启用不同。
1、HUP中断信号:HUP中断信号的对应操作为让进程挂起,睡眠。同<Ctrl+X>
2、INT中断信号:INT中断信号的对应操作为正常关闭所有进程。同<Ctrl+C>
3、TERM中断信号 15:TERM中断信号的对应操作为正常的退出进程。
4、KILL中断信号 9:KILL中断信号的对应操作为强制关闭进程。
5、STOP 19暂停(同 Ctrl + Z)
6、CONT 18继续(与STOP相反, fg/bg命令)
7、TSTP中断信号:TSTP中断信号的对应操作为暂时停用进程。

# 代码
[root@jh shell]# cat jumpserver.sh
#!/bin/bash
 
cat<<EOF
1. BACKUP 10.0.0.41
2. WEB02  192.168.12.21
3. WEB03  10.0.0.9
EOF
trap "echo 不要乱按键盘,否则服务器将会爆炸" HUP INT TSTP
while true
do
  read -p "请输入连接主机编号信息: " num
  read -p "请输入账号: " user
  case $num in
   1)
     ssh [email protected]
     [ $? -ne 0 ] && echo "connect faild"
     ;;
   2)
     ssh [email protected]
     [ $? -ne 0 ] && echo "connect faild"
     ;;
   *)
     echo "请输入连接主机信息"
  esac
done

标签:case,语句,nginx,stop,echo,start,第五章,bin,root
From: https://www.cnblogs.com/GAO321/p/16711718.html

相关文章

  • Mysql系列---【使用慢日志查询分析sql语句】
    1.查看慢日志是否开启mysql>showvariableslike'slow_query%';+---------------------------+----------------------------------+|Variable_name|......
  • javascript中的控制语句
      1、forin用来遍历对象的,可以在属性未知的情况下遍历对象  2、forof:遍历数组和其他迭代对象,如:Map,Set等  3、trycatchfinally,捕获异常,无论异常是否发......
  • shell脚本循环语句
    shell脚本(shellscript)的用处:shell是一种命令语言,本质上也可以说是一种C语言程序,可以帮助我们更方便使用linux,shell和shell脚本不同,工作中,我们一般用到的都是shell脚本,在......
  • delete语句嵌套-mysql
    需求:我需要从tableA之中找到name字段的Filter的记录,得到此记录的id,然后根据id删除tableA中的该条记录;--但是我使用如下xql语句的时候deletefromtableAwhereidin(s......
  • ClickHouse(06)ClickHouse的数据表创建语句详细解析
    目录当前服务器上创建表(单节点)语法形式使用显式架构从相同结构的表复制创建从表函数创建从选择查询创建分布式集群创建表临时表分区表创建表语句关键字解析空值或非空修......
  • JavaScript break 和 continue 语句
    break语句用于跳出循环。continue用于跳过循环中的一个迭代。break语句break语句可用于跳出循环。break语句跳出循环后,会继续执行该循环之后的代码continue语句......
  • JavaScript if...Else 语句
    if语句      语法二:        if(条件表达式){          语句……        }else{     ......
  • JavaScript switch 语句
    语法switch(n){case1:执行代码块1break;case2:执行代码块2break;default:与case1和case2不同时执行的代码} n通常为变量,随后表达式的值会......
  • MySql 表 转为C#实体类 ,sql语句
    装载自:https://www.cnblogs.com/noobprogrammer/p/15745382.html SELECT CONCAT( '///<summary>\r\n///', COLUMN_COMMENT, '\r\n///</summary>\r\npublic', CAS......
  • MySQL基础架构:SQL查询语句执行过程
    MySQL是一种关系型数据库管理系统,采用的是分层结构,本文中将简单介绍MySQL数据库的内部架构。目录客户端层服务器层连接器查询缓存分析器优化器执行器存储层MySQL是一个C/......