shell脚本case_模式匹配详解

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 在某种意义上,case语句是if语句的简洁版,case语句适合做字符串模式匹配,如果不同的字符值对应不同的功能则用case语句实现,case无法做比较以及测试命令,最后一个模式可以省略;;command -v 命令用来测试是否是一个命令看$?返回值即可1.1根据系统版本匹配yum源文件

case 模式匹配

一、case语法结构

case 变量名 in
模式1)
​ 命令序列1
​ ;;
模式2)
​ 命令序列2
​ ;;
模式3)
​ 命令序列3
​ ;;
*)
​ 无匹配后命令序列
esac

在某种意义上,case语句是if语句的简洁版,case语句适合做字符串模式匹配,如果不同的字符值对应不同的功能则用case语句实现,case无法做比较以及测试命令,最后一个模式可以省略;;

command -v 命令用来测试是否是一个命令看$?返回值即可

1.1根据系统版本匹配yum源文件

#!/bin/bash
yum_server=192.168.81.250
os_version=$(cat /etc/redhat-release |awk '{print $(NF-1)}'| awk -F '.' '{print $1"."$2}')
[ -d /etc/yum.repos.d/bak ] || mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/* /etc/yum.repos.d/bak &>/dev/null
case "$os_version" in
"7.6")
        cat > /etc/yum.repos.d/centos7u6.repo <<-EOF
        [centos7u6]
        name=centos7u6
        baseurl=file:///media
        enabled=1
        gpgcheck=0      
        EOF
        echo "$os_version yum config finish...."
        ;;
"6.5")
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
        echo "$os_version yum config finish...."
        ;;
"5.4")
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
        echo "$os_version yum config finish...."
        ;;
*)
        echo "error os_version"
esac
echo "finish.."

1.2删除用户,根据输入的字符确认是否删除

#!/bin/bash
-------------------if实现-----------------
read -p "please input a delete username: " user
id $user &>/dev/null
if [ $? -ne 0 ];then
        echo "no such user: $user"
        exit 1        //设置退出状态码
fi
read -p "are you sure delete: $user [y/n]" action
if [ "$action" = y -o "$action" = "yes" -o "$action" = "Y" -o "$action" = "YES" ];then
        userdel -r $user &>/dev/null
        echo "$uer is del..."
else
        echo "you is quit"
fi
-------------------------case实现-----------------
read -p "please input a delete username: " user
id $user &>/dev/null
if [ $? -ne 0 ];then
        echo "no such user: $user"
        exit 1
fi
read -p "are you sure delete: $user [y/n]" action
case $action in
y|Y|yes|YES)
        userdel -r $user &>/dev/null
        echo "$user is del..."
        ;;
*)
        echo "you is quit"
esac

1.3测试某个文件是否是一个命令

#!/bin/bash
command1=/bin/date
if  command -v $command1 &>/dev/null;then
        :   
else
        echo " $command is not keywords"
fi

1.4利用case+if实现批量创建用户

#!/bin/bash
read -p "please input prefix: " prefix
if [ -z $prefix ];then
        echo "error prefix"
        exit 1
fi
read -p "please input create num: " num
if [[ ! $num =~ ^[0-9]+$ ]];then
        echo "error num"
        exit 2
fi
read -p "are you sure create {prefix+num} user[y/n]: " action
case $action in
y|Y|yes|YES)
        echo "begin create....."
        for i in `seq $num`
        do
                user=$prefix$i
                useradd $user &>/dev/null
                if [ $? -eq 0 ];then
                        echo "123" | passwd --stdin $user &>/dev/null
                fi
                echo "$user is add....."
        done
        ;;
n|N|no|NO)
        echo "scripts is quit...."
esac

1.4利用case+if实现批量删除用户

#!/bin/bash
read -p "please input prefix: " prefix
if [ -z $prefix ];then
        echo "error prefix"
        exit 1
fi
read -p "please input delete num: " num
if [[ ! $num =~ ^[0-9]+$ ]];then
        echo "error num"
        exit 2
fi
read -p "are you sure delete {prefix+num} user[y/n]: " action
case $action in
y|Y|yes|YES)
        echo "begin delete....."
        for i in `seq $num`
        do
                user=$prefix$i
                userdel -r $user &>/dev/null
                echo "$user is del....."
        done
        ;;
n|N|no|NO)
        echo "scripts is quit...."
esac

1.5case实现编译安装软件

#!/bin/bash
###############################################################################################
#                           #
# case编译安装工具箱  jxl--20200225                  #
#                                                                                             #
###############################################################################################
tools_dir=/software 
apache_tools_dir=/software/httpd-2.2.17.tar.gz
mysql_tools_dir=/software/mysql-5.5.22.tar.gz
cmake_tools_dir=/software/cmake-2.8.6.tar.gz
nginx_tools_dir=/software/nginx-1.6.0.tar.gz
echo -e "\033[33m begin checking software...\033[0m"
yum list installed | grep 'gcc-c++' &>/dev/null
if [ $? -ne 0 ];then
  read -p "system not gcc,do you install gcc-c++? [y|n]: " action
  case $action in 
  y|Y) 
    yum -y install gcc-c++
    if [ $? -eq 0 ];then
      echo -e "\033[32m gcc-c++ is installed... \033[0m"
    fi
    ;;
  n|N)
    echo -e "\033[31m no gcc-c++ not install tools... \033[0m"
    echo -e "\033[31m scripts is quit... \033[0m"
    exit 1
    ;;
  esac  
fi
echo -e "\033[31m Can install the tool:[httpd|mysql|nginx] \033[0m"
read -p "please input install tool: " tool
case $tool in 
httpd)
  read -p "are you sure install apache_httpd [y|n]: " action_httpd
  if  [ "$action_httpd" = "y" -o "$action_httpd" = "yes" ];then
    yum -y remove httpd
    tar xvf $apache_tools_dir  -C $tools_dir
    cd $tools_dir/httpd-2.2.17
    ./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi && make && make install
    ln -s /usr/local/httpd/bin* /usr/local/bin &>/dev/null
    cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
    sed -i -e '1a # chkconfig:35 85 21' /etc/init.d/httpd
    sed -i -e '2a # description:Startup script for the Apache HTTP Server' /etc/init.d/httpd
    systemctl start httpd 
    systemctl enable httpd
    netstat -lnpt | grep httpd
    if [ $? -eq 0 ];then
      echo -e "\033[32m httpd installed....\033[0m"
    fi
  elif [ "$action_httpd" = "n" -o "$action_httpd" = "no" ];then
    echo -e "\033[31m scripts is quit... \033[0m"
    exit 1
  else
    echo -e "\033[31m error action \033[0m" 
    exit 2
  fi
  curl -I 127.0.0.1 &>/dev/null
  if [ $? -eq 0 ];then
    echo -e "\033[32m web page is ok...\033[0m"
  fi
  ;;
mysql)
  read -p "are you sure install mysql [y|n]: " action_mysql
  if  [ "$action_mysql" = "y" -o "$action_mysql" = "yes" ];then
    yum -y remove mariadb
    yum -y install ncurses-devel
    tar xvf $cmake_tools_dir -C $tools_dir
    cd $tools_dir/cmake-2.8.6
    ./configure && make && make install
    tar -xvf $mysql_tools_dir -C $tools_dir
    cd $tools_dir/mysql-5.5.22
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWICH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make &&make install
    \cp -rf support-files/my-medium.cnf /etc/my.cnf
    \cp -rf support-files/mysql.server /etc/init.d/mysqld
    chmod +x /etc/init.d/mysqld
    ln -s /usr/local/mysql/bin/* /usr/local/bin &>/dev/null
    groupadd mysql &>/dev/null
    useradd -M -s /sbin/nologin -g mysql mysql &>/dev/null
    chown -R mysql:mysql /usr/local/mysql
    /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
    /etc/init.d/mysqld start
    netstat -lnpt | grep mysql
    if [ $? -eq 0 ];then
      echo -e "\033[32m mysqld installed...\033[0m"
    fi
  elif [ "$action_mysql" = "n" -o "$action_mysql" = "no" ];then
    echo -e "\033[31m scripts is quit... \033[0m"
    exit 1
  else
    echo -e "\033[31m error action... \033[0m"  
    exit 2
  fi
  ;;
nginx)
  read -p "are you sure install mysql [y|n]: " action_nginx
  if  [ "$action_nginx" = "y" -o "$action_nginx" = "yes" ];then
    yum -y install  openssl-devel zlib-devel pcre-devel 
    tar -xvf $nginx_tools_dir -C $tools_dir
    cd $tools_dir/nginx-1.6.0
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module  && make && make install
    netstat -lnpt | grep 80
    if [ $? -eq 0 ];then
      echo -e "\033[32m port 80 is used...\033[0m"
      echo -e "\033[33m begin update port is 8888!\033[0m"
      sed -i 's/80;/8888;/' /usr/local/nginx/conf/nginx.conf
      if [ $? -eq 0 ];then
        echo -e "\033[33m  port is 8888...\033[0m"
      fi
    fi
    ln -s /usr/local/nginx/sbin/* /usr/local/sbin &>/dev/null
    nginx 
    netstat -lnpt | grep nginx 
    if [ $? -eq 0 ];then
      echo  -e "\033[32m nginx installed...\033[0m"
    fi
   elif [ "$action_nginx" = "n" -o "$action_nginx" = "no" ];then
    echo -e "\033[31m scripts is quit... \033[0m"
    exit 1
     else
        echo -e "\033[31m error action... \033[0m"      
        exit 2
  fi
  ;;
*)
  echo -e "\033[31m The tool is still in development...\033[0m"
esac

1.6case实现简单的跳板机

跳板机适用场景:


1.业务服务器不允许直接连接,通过跳板机进行连接


2.业务服务器不允许root用户登录


跳板机原理


由客户端登录到跳板机服务器,跳板机服务器根据用户输入的ip远程连接对应的应用服务器,客户端不能直接与应用服务器直连,所有主机准备远程连接时使用的账号密码,并设置为密码验证或者密钥验证


此案例需求:


1.做出一个服务器列表


2.根据用户的输入判断去连接哪一台服务器


3.用户连到跳板机服务器就已经将脚本运行好


4.连接进去后不允许退出

#!/bin/bash
clear
trap "" INT HUP OUIT TSTP
while :
do
        cat <<-EOF
                +---------------------------------------------------------+
                |                       jumpserver                        |
                |                       1.192.168.81.210                  |
                |                       2.192.168.81.211                  |
                |                       3.192.168.81.212                  |
                |                       4.192.168.81.213                  |
                +---------------------------------------------------------+
        EOF
        echo -en "\e[33m please input connection host: \e[0m"
        read host
        case  $host in
        1)
                ssh jxl@192.168.81.210
                ;;
        2)
                ssh jxl@192.168.81.211
                ;;
        3)
                ssh jxl@192.168.81.212
                ;;
        4)
                ssh jxl@192.168.81.213
                ;;
        "")
                ;;
        *)
                echo "The host does not exist"
         esac
done

1.7case实现系统管理工具箱

#!/bin/bash
menu(){
cat <<-EOF
+------------------------------------------------------------+
|               系统工具箱                                   |
|               h.help                                       |
|               b.磁盘分区情况                               |
|               d.磁盘使用情况                               |
|               m.内存使用情况                               |
|               u.系统负载情况                               |
|               t.CPU使用情况                                |
|               q.exit                                       |
+------------------------------------------------------------+
EOF
}
clear
while :
do
        menu
        echo  -en "\e[33m Please choose to use tool: \e[0m"
        read  tools
        case $tools in
        h)
                echo "这是一个系统管理工具箱,可以根据所需选择对应的工具"
                menu
                ;;
        b)
                lsblk
                ;;
        d)
                df -hT
                ;;
        m)
                free -g
                ;;
        u)
                uptime
                ;;
        t)       
            top
            ;;
        q)
                exit
                ;;
        "")
                ;;
        *)
                echo -e "\e[31m error option \e[0m"
        esac
done
目录
相关文章
|
1天前
|
分布式计算 Hadoop Shell
使用shell脚本实现自动SSH互信功能
使用shell脚本实现自动SSH互信功能
11 1
|
1天前
|
Unix Shell Linux
轻松编写 AIX Shell 脚本
轻松编写 AIX Shell 脚本
14 1
|
1天前
|
监控 关系型数据库 Shell
Shell脚本入门:从基础到实践,轻松掌握Shell编程
Shell脚本入门:从基础到实践,轻松掌握Shell编程
|
1天前
|
关系型数据库 MySQL Shell
在Centos7中利用Shell脚本:实现MySQL的数据备份
在Centos7中利用Shell脚本:实现MySQL的数据备份
|
1天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
30 5
|
1天前
|
Shell 程序员 数据安全/隐私保护
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
shell 脚本 if-else判断 和流程控制 (基本语法|基础命令)
|
1天前
|
存储 Shell C语言
shell脚本 编程 变量 基本入门(详解)
shell脚本 编程 变量 基本入门(详解)
|
1天前
|
Shell 索引
shell脚本入门到实战(四)- 数组
shell脚本入门到实战(四)- 数组
|
1天前
|
Shell
shell脚本入门到实战(三) - 变量
shell脚本入门到实战(三) - 变量
|
1天前
|
Shell Linux 人机交互
shell脚本入门到实战(二)--shell输入和格式化输出
shell脚本入门到实战(二)--shell输入和格式化输出