zoukankan      html  css  js  c++  java
  • cat EOF追加与覆盖

    root@fadfdd4af58a:~# cat /etc/resolv.conf 
    options timeout:2 attempts:5
    ; generated by /usr/sbin/dhclient-script
    search us-east-2.compute.internal us-west-2.compute.internal
    nameserver 172.31.0.2
    root@fadfdd4af58a:~# cat << EOF > /etc/resolv.conf
    > nameserver 8.8.8.8
    > EOF
    root@fadfdd4af58a:~# cat /etc/resolv.conf         
    nameserver 8.8.8.8
    root@fadfdd4af58a:~# 

    当需要将多行文件输入到文本时,如果每条都使用echo 到文件时是比较繁琐的,这种情况下可以使用cat EOF进行多行文件的覆盖或追加输入。

    一、覆盖

    这里有两种格式可以使用

    1、格式一

    #!/bin/bash
    cat << EOF > /root/test.txt
    Hello!
    My site is www.361way.com
    My site is www.91it.org
    Test for cat and EOF!
    EOF

    2、格式二

    #!/bin/bash
    cat > /root/test.txt <<EOF
    Hello!
    My site is www.361way.com
    My site is www.91it.org
    Test for cat and EOF!
    EOF

    两种写法区别无法是要写入的文件放在中间或最后的问题,至于选哪种看个人喜好吧。

    二、追加

    覆盖的写法基本和追加一样,不同的是单重定向号变成双重定向号。

    1、格式一

    #!/bin/bash
    cat << EOF >> /root/test.txt
    Hello!
    My site is www.361way.com
    My site is www.91it.org
    Test for cat and EOF!
    EOF

    2、格式二

    #!/bin/bash
    cat >> /root/test.txt <<EOF
    Hello!
    My site is www.361way.com
    My site is www.91it.org
    Test for cat and EOF!
    EOF

    需要注意的是,不论是覆盖还是追加,在涉及到变量操作时是需要进行转义的,例如:

    #!/bin/bash
    cat <<EOF >> /root/a.txt
    PATH=$PATH:$HOME/bin
    export ORACLE_BASE=/u01/app/oracle
    export ORACLE_HOME=$ORACLE_BASE/10.2.0/db_1
    export ORACLE_SID=yqpt
    export PATH=$PATH:$ORACLE_HOME/bin
    export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
    EOF

     

  • 相关阅读:
    探索Java8:(二)Function接口的使用
    Vue 动态图片加载路径问题和解决方法
    小工具:使用Python自动生成MD风格链接
    解决Navicat Premium 12 连接oracle数据库出现ORA-28547的问题
    Beetl模板引擎入门教程
    JSON.stringify()的深度使用
    nvm 查看node版本
    去掉点击a标签时产生的虚线框
    html 设置input框的记忆功能(联想内容)
    php post和get请求
  • 原文地址:https://www.cnblogs.com/dream397/p/13785648.html
Copyright © 2011-2022 走看看