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

     

  • 相关阅读:
    团队项目——需求规格说明书
    UML
    第二次团队作业——预则立&&他山之石
    团队选题报告
    团队展示
    ubuntu下使用pyinstaller将python文件打包成exe(py文件中含有keras框架的代码)
    ubuntu 为项目配置虚拟环境
    安装cuda9.0 + cudnn7.0 与 cuda8.0共存
    ubuntu 16.04 + cuda8.0 + cudnn 6.1 + tensorflow 1.4.0
    修改vs2013中MFC项目名称
  • 原文地址:https://www.cnblogs.com/dream397/p/13785648.html
Copyright © 2011-2022 走看看