shell脚本小例

这是一个用于管理Redis服务的自动化脚本,包括安装、启动、停止、卸载等操作。用户可以选择查看版本信息、安装Redis并启动、卸载Redis等。卸载过程会提示确认,并在成功完成后返回菜单。此外,还有查看状态和设置开机启动等功能。
摘要由CSDN通过智能技术生成

这是一个安装卸载redis的脚本

#! /bin/bash

function redisInstall(){

 echo "
     ----------------redis-----------------
     1、Redis Version
     2、Redis Install&start
     3、Redis Stop&Remove
     4、Redis Start
     5、Redis Stop
     6、Redis Enable
     7、Redis Disable
     8、Redis Status
     9、Exit
     0、Menu
    "
    
 while true 
 do
    read -p "input info:" char
    case ${char} in
    1)
      #
      if ! test -z "$(find /usr/local/bin/ -name redis-server)" || ! test -z "$(find /usr/bin/ -name redis-server)" ;then
      echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      redis-server -v
      echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      else
      echo "Exception: Version information does not exist!"
      fi
    ;;
    2)
      yum install -y redis && systemctl start redis
    ;;
    3)
      read -p "Are you sure you want to uninstall redis???(yes/no | y/n)": yesOrNo
      if [ "${yesOrNo}" =  "yes" -o "${yesOrNo}" =  "y" ];
       then
        i=0
      while true;do
       sleep 0.1
       let i++
       if [ "${i}" -eq 80 ];then
         break
       fi
         echo -n "."
      done
      echo "start Uninstall!!!"
      systemctl stop redis && yum remove -y redis && rm -rf /var/lib/redis /var/log/redis /usr/local/bin/redis-server /usr/bin/redis-server && echo "Uninstall complete!" || echo "Exception: Service does not exist!"
      elif [ "${yesOrNo}" =  "no" -o "${yesOrNo}" =  "n" ];
       then
:<<EOF i=0
      while true;do
       sleep 0.6
       let i++
       if [ "${i}" -eq 5 ];then
         break
       fi
         echo -n "."
      done
      sh /app/nas/redisStatusInfo.sh
      else
EOF
      sh /app/nas/redisStatusInfo.sh
      fi

    ;;
    4)
      systemctl start redis || echo "Exception: Service does not exist!"
    ;;
    5)
      systemctl stop redis  || echo "Exception: The service is not started or does not exist!"
    ;;
    6)
      systemctl enable redis || echo "Exception: The service does not exist or is abnormal!"
    ;;
    7)
      systemctl disable redis || echo "Exception: The service does not exist or is abnormal!"
    ;;
    8)
      systemctl status redis || echo "Exception: The service is not started or does not exist!"
    ;; 
    9)
      exit
    ;;
    0)
      sh /app/nas/redisStatusInfo.sh
    ;;
    *)

    if [ ! -z "${char}" ];then
    echo "warning!"
    fi

    esac
done

}


redisInstall

注释:

#! /bin/bash

function redisInstall(){
#菜单
 echo "
     ----------------redis-----------------
     1、Redis Version
     2、Redis Install&start
     3、Redis Stop&Remove
     4、Redis Start
     5、Redis Stop
     6、Redis Enable
     7、Redis Disable
     8、Redis Status
     9、Exit
     0、Menu
    "
#定义死循环    
 while true 
 do #输入
    read -p "input info:" char
    case ${char} in #分支语句
    1)
      #查找并判断是否存在redis-server
      if ! test -z "$(find / -name redis-server)" ;then
      echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      redis-server -v#打印版本信息
      echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Version~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
      else#异常
      echo "Exception: Version information does not exist!"
      fi
    ;;
    2)#安装&启动&开启启动
      yum install -y redis && systemctl start redis && systemctl enable redis
    ;;
    3)#输入 询问是否删除 
      read -p "Are you sure you want to uninstall redis???(yes/no | y/n)": yesOrNo
      if [ "${yesOrNo}" =  "yes" -o "${yesOrNo}" =  "y" ];#判断输入yes还是y
       then
        i=0
      while true;do #定义死循环
       sleep 0.1 #0.1毫秒
       let i++ #每0.1毫秒增加一个.点
       if [ "${i}" -eq 80 ];then #增加到第80个点时终止循环
         break #终止循环
       fi
         echo -n "." #输出.点
      done
      echo "start Uninstall!!!" #开始卸载
      #关闭&卸载&删除残留文件
      systemctl stop redis && yum remove -y redis && rm -rf /var/lib/redis /var/log/redis /usr/local/bin/redis-server /usr/bin/redis-server && echo "Uninstall complete!" || echo "Exception: Service does not exist!"
      elif [ "${yesOrNo}" =  "no" -o "${yesOrNo}" =  "n" ];#判断输入no还是n
       then
:<<EOF i=0 #注释
      while true;do
       sleep 0.6
       let i++
       if [ "${i}" -eq 5 ];then
         break
       fi
         echo -n "."
      done
      sh /app/nas/redisStatusInfo.sh
      else
EOF
      #卸载完毕回到菜单
      sh ./redisStatusInfo.sh
      fi

    ;;
    4)#启动redis
      systemctl start redis || echo "Exception: Service does not exist!"
    ;;
    5)#关闭redis
      systemctl stop redis  || echo "Exception: The service is not started or does not exist!"
    ;;
    6)#redis开机启动
      systemctl enable redis || echo "Exception: The service does not exist or is abnormal!"
    ;;
    7)#关闭开机启动
      systemctl disable redis || echo "Exception: The service does not exist or is abnormal!"
    ;;
    8)#查看redis状态
      systemctl status redis || echo "Exception: The service is not started or does not exist!"
    ;; 
    9)
      exit
    ;;
    0)#回到主菜单
      sh ./redisStatusInfo.sh
    ;;
    *)
    #输入内容不在分支选项内
    if [ ! -z "${char}" ];then
    echo "warning!"
    fi

    esac
done

}

#执行方法
redisInstall
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值