cat EOF追加与覆盖

cat EOF追加与覆盖

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

当需要将多行文件输入到文本时,如果每条都使用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

 
posted @ 2019-08-16 10:35  翎戍  阅读(728)  评论(0编辑  收藏  举报