Linux shell编程自动化运维 判断,case 模式匹配 , 判断总结 详细解析

case模式匹配

case 语法结构

一、case 语法结构(字符串比较)
case 变量 in
模式1)
命令序列1
;;
模式2)
命令序列2
;;
模式3)
命令序列3
;;
*)
无匹配后命令序列
esac

示例1
编写脚本,由用户输入字符串,如果输入的字符串为Linux则显示Windows,为Windows则显示Linux,否则显示other

#!/bin/bash

read -p "Input string: " str

case $str in
windows)
        echo "Linux..."
;;
linux)
        echo "Windows..."
;;
*)
        echo "Other...."
;;
esac

示例2
1.根据不同的系统版本,使用 if 配置yum源,
2.快速修改脚本为case
if 语句示例

os_version=`cat /etc/redhat-release | awk '{print $4}'|awk -F"." '{print $1}' `
if [ "$os_version" = "7" ];then
        cat > /etc/yum.repos.d/centos7.repo <<-EOF
        [centos]
        name=centos7
        baseurl=ftp://10.18.40.100/centos7u3
        gpgcheck=0
        EOF
        yum makecache
        yum repolist
        echo "yum Configuration completed"
fi
if [ "$os_version" = "6" ];then
        cat > /etc/yum.repos.d/centos6.repo <<-EOF
        [centos]
        name=centos6
        baseurl=ftp://10.18.40.100/centos6u2
        gpgcheck=0
        EOF
        yum makecache
        yum repolist
        echo "
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值