• Shell脚本使用sed命令时遇到变量的一些解决方案


    关于SED命令我就不多说了,下面奉上我的例子

    在写脚本配置YUM源的时候,总是以多多益善的想法为主,所以这时候就需要制定一个模版了,下面奉上我的Centos7源的模版

    [base]
    name=CentOS-$releasever - Base - URLS
    failovermethod=priority
    baseurl=URLS/centos/$releasever/os/$basearch/
            URLS/centos/$releasever/os/$basearch/
            URLS/centos/$releasever/os/$basearch/
    gpgcheck=1
    gpgkey=URLS/centos/RPM-GPG-KEY-CentOS-7
     
    #released updates 
    [updates]
    name=CentOS-$releasever - Updates - URLS
    failovermethod=priority
    baseurl=URLS/centos/$releasever/updates/$basearch/
            URLS/centos/$releasever/updates/$basearch/
            URLS/centos/$releasever/updates/$basearch/
    gpgcheck=1
    gpgkey=URLS/centos/RPM-GPG-KEY-CentOS-7
     
    #additional packages that may be useful
    [extras]
    name=CentOS-$releasever - Extras - URLS
    failovermethod=priority
    baseurl=URLS/centos/$releasever/extras/$basearch/
            URLS/centos/$releasever/extras/$basearch/
            URLS/centos/$releasever/extras/$basearch/
    gpgcheck=1
    gpgkey=URLS/centos/RPM-GPG-KEY-CentOS-7
     
    #additional packages that extend functionality of existing packages
    [centosplus]
    name=CentOS-$releasever - Plus - URLS
    failovermethod=priority
    baseurl=URLS/centos/$releasever/centosplus/$basearch/
            URLS/centos/$releasever/centosplus/$basearch/
            URLS/centos/$releasever/centosplus/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=URLS/centos/RPM-GPG-KEY-CentOS-7
     
    #contrib - packages by Centos Users
    [contrib]
    name=CentOS-$releasever - Contrib - URLS
    failovermethod=priority
    baseurl=URLS/centos/$releasever/contrib/$basearch/
            URLS/centos/$releasever/contrib/$basearch/
            URLS/centos/$releasever/contrib/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=URLS/centos/RPM-GPG-KEY-CentOS-7
    Centos7源模版

    在这个模版中,URLS属于我预设的镜像URL可替换变量名,所以我的思路就是只需要将这个变量名替换成一个可用的URL地址即可使用该源,但是这时候谁也不知道要将这个URLS替换成什么

    所以这时候就需要使用变量来解决了,一开始的想法是将不确定的URL赋值到变量U里面,然后执行下面的指令

    sed "s/URLS/$U/g" dome.repo

     然后提示错误了,于是我查了一下资料,得知这个SED指令可以自定义分隔符,因为   在shell中是有特殊的含义的,所以这时候只需要把这个符号换成其他的即可,比如#

     这时候就神奇的成功了,然后试一下&

     结果可见,涉及变量的时候只需要把斜杠换成其他符号即可

  • 相关阅读:
    软件工程-个人最终总结
    结对编程—电梯调度
    第三周(第三作业)感想
    周三第二个作业
    VS2013安装和单元测试
    对京东的评价
    简单的四则运算
    迷茫的软件工程
    vlan 和 子网
    ECLIPSE的jar包和文件的导入导出
  • 原文地址:https://www.cnblogs.com/liuyi778/p/12295161.html
Copyright © 2020-2023  润新知