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

    cat EOF追加与覆盖

    2011年10月25日 发表评论 阅读评论
     

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

    一、覆盖

    这里有两种格式可以使用

    1、格式一

    1. #!/bin/bash
    2. cat << EOF > /root/test.txt
    3. Hello!
    4. My site is www.361way.com
    5. My site is www.91it.org
    6. Test for cat and EOF!
    7. EOF

    2、格式二

    1. #!/bin/bash
    2. cat > /root/test.txt <<EOF
    3. Hello!
    4. My site is www.361way.com
    5. My site is www.91it.org
    6. Test for cat and EOF!
    7. EOF

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

    二、追加

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

    1、格式一

    1. #!/bin/bash
    2. cat << EOF >> /root/test.txt
    3. Hello!
    4. My site is www.361way.com
    5. My site is www.91it.org
    6. Test for cat and EOF!
    7. EOF

    2、格式二

    1. #!/bin/bash
    2. cat >> /root/test.txt <<EOF
    3. Hello!
    4. My site is www.361way.com
    5. My site is www.91it.org
    6. Test for cat and EOF!
    7. EOF

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

    1. #!/bin/bash
    2. cat <<EOF >> /root/a.txt
    3. PATH=$PATH:$HOME/bin
    4. export ORACLE_BASE=/u01/app/oracle
    5. export ORACLE_HOME=$ORACLE_BASE/10.2.0/db_1
    6. export ORACLE_SID=yqpt
    7. export PATH=$PATH:$ORACLE_HOME/bin
    8. export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
    9. EOF

     出处:http://www.361way.com/cat-eof-cover-append/4298.html

     
  • 相关阅读:
    Elasticsearch 结构化搜索
    KMP 算法
    ElasticSearch 配置
    C++ 入门
    Spark 基础操作
    HBase 与 MapReduce 集成
    iOS面试相关
    iOS开发值得学习的Demo
    Mac系统安装MyEclipse
    linux安装tomcat
  • 原文地址:https://www.cnblogs.com/lingshu/p/11362381.html
Copyright © 2011-2022 走看看