在shell中,文件分界符(通常写成EOF,你也可以写成FOE或者其他任何字符串)紧跟在<<符号后,意思是分界符后的内容将被当做标准输入传给<<前面的命令,直到再次在独立的一行遇到这个文件分界符(EOF或者其他任何字符,注意是独立一行,EOF前面不能有空格)。通常这个命令是cat,用来实现一些多行的屏幕输入或者创建一些临时文件。

1、最简单的用法
root@ribbonchen-laptop:~# cat<<EOF
> ha
> haha
> hahaha
> EOF
输出:
ha
haha
hahaha
2、把输出追加到文件
root@ribbonchen-laptop:~# cat<<EOF>out.txt
> ha
> haha
> hahaha
> EOF
root@ribbonchen-laptop:~# cat out.txt
ha
haha
hahaha
3、换一种写法
root@ribbonchen-laptop:~# cat>out.txt<<EOF
> ha
> haha
> hahaha
> EOF
root@ribbonchen-laptop:~# cat out.txt
ha
haha
hahaha
4、cat>filename,创建文件,并把标准输入输出到filename文件中,以ctrl+d作为输入结束
root@ribbonchen-laptop:~# cat>filename
ha  
haha 
hahaha
root@ribbonchen-laptop:~# cat filename
ha
haha
hahaha
 
下面的脚本实现了一个简单的菜单功能:
#!/bin/bash
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname`
USER=`whoami`
while :
do
  clear
  cat<<EOF
  _______________________________________________________________
  User:$USER        Host:$THIS_HOST          DATE:$MYDATE
  _______________________________________________________________
                 1:List files in current dir
                 2:Use the vi editor
                 3:See who is on the system
                 H:Help sreen
                 Q:Exit Menu
  _______________________________________________________________
EOF
  echo -e -n "\tYour Choice [1,2,3,H,Q]>"
  read CHOICE
    case $CHOICE in
    1) ls
      ;;
    2) vi
      ;;
    3) who
      ;;
    H|h)
      cat<<EOF
      This is the help screen,nothing here yet to help you!
EOF
      ;;
    Q|q) exit 0
      ;;
    *) echo -e "\t\007unknown user response"
      ;;
    esac
  echo -e -n "\tHit the return key to continue"
  read DUMMY
done
 
 
 
 
#!/bin/bash
TARGET_DIR=$PWD
cd /
list=`cat << EOF
usr/local/Trolltech/QtEmbedded-4.6.3-arm/examples/widgets/wiggly/wiggly
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/screenshots/raycasting.png usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_6.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_3.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/slides/demo_5.png
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/fluidlauncher
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/fluidlauncher/config.xml
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/embedded/styledemo/styledemo
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/pathstroke/pathstroke.html
usr/local/Trolltech/QtEmbedded-4.6.3-arm/demos/pathstroke/pathstroke
EOF
`
tar cfvz $TARGET_DIR/target-qte-4.6.3.tgz $list
EOF本意是 End Of File,表明到了文件末尾。

使用格式基本是这样的: 
命令 << EOF
内容段
EOF
将“内容段”整个作为命令的输入。
你的代码里就是用cat命令读入整段字符串并赋值给list变量。
其实,不一定要用EOF,只要是“内容段”中没有出现的字符串,都可以用来替代EOF,只是一个起始和结束的标志罢了。 有个特殊用法不得不说:
: << COMMENTBLOCK
shell脚本代码段
COMMENTBLOCK
这个用来注释整段脚本代码。 : 是shell中的空语句。 搜索一下 Here document 你会明白更多。
如果就一行,那么直接赋值即可:
list="usr/local/Trolltech/QtEmbedded-4.6.3-arm/examples/widgets/wiggly/wiggly" EOF相当于读文件的方式,适用于多行内容的操作。

shell中的cat和文件分界符(<<EOF)的更多相关文章

  1. shell中的cat和文件分界符(<<EOF) (转)

    原文地址: http://blog.csdn.net/mosesmo1989/article/details/51123257 在shell中,文件分界符(通常写成EOF,你也可以写成FOE或者其他任 ...

  2. 在bash shell中使用getfattr查看文件扩展属性

    getfattr用法 用于获取文件扩展属性,返回一系列键值对,参考Linux Man Page. 常用OPTIONS -n name, --name=name Dump the value of th ...

  3. shell中通过sed替换文件中路径

    通常sed指令修改行内容时使用:sed -i " 9 s/^.*/"type in what you want modified!"/" 其中"typ ...

  4. 对于Linux中文件描述符的疑问以及解决

    问题 ​ 每次web服务器或者是几乎所有Linux服务器都需要对文件描述符进行调整,我使用ulimit -n来查看当前用户的最多能打开的文件,默认设置的是1024个,但是系统运行起来以及开启一些简单的 ...

  5. 【Shell脚本学习指南笔记】重定向文件描述符 2>&1

    如: make > results 2>&1 重定向 > results让文件描述符1(标准输出)作为文件results,接下来的重定向2>&1有两个部分.2& ...

  6. Shell中bash的特性小结

    Shell: 用户与操作系统之间完成交互式操作的一个接口程序,为用户提供简化了的操作:上世纪的70年代中期在贝尔实验室,Bourne位Unix开发了一个shell程序Bourne Shell,简称sh ...

  7. exec操作文件描述符

    exec命令可以用来替代当前shell:换句话说,并没有启动子shell.使用这一命令时任何环境都将被清除,并重新启动一个shell. 它的一半形式为: exec command 其中,command ...

  8. shell学习(17)- shell中2>&1的解释及输入输出重定向

    大多数 UNIX 系统命令从你的终端接受输入并将所产生的输出发送回​​到您的终端.一个命令通常从一个叫标准输入的地方读取输入,默认情况下,这恰好是你的终端.同样,一个命令通常将其输出写入到标准输出,默 ...

  9. Unix系统编程()复制文件描述符

    Bourne shell的IO重定向语法2>&1,意在通知shell把标准错误(文件描述符2)重定向到标准输出(文件描述符1).因此下列命令将把标准输出和标准错误写入result.log ...

随机推荐

  1. 一.HttpClient、JsonPath、JsonObject运用

    HttpClient详细应用请参考官方api文档:http://hc.apache.org/httpcomponents-client-4.5.x/httpclient/apidocs/index.h ...

  2. Codeforces Round #346 (Div. 2) A Round-House

    A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...

  3. 7. Shell 函数

    1. 格式 [ function ] funname [()] { action; [return int;] } 可以带function fun() 定义,也可以直接fun() 定义,不带任何参数 ...

  4. 为什么要lock,lock了什么?

    当我们使用线程的时候,效率最高的方式当然是异步,即各个线程同时运行,其间不相互依赖和等待.但当不同的线程都需要访问某个资源的时候,就需要同步机制了,也就是说当对同一个资源进行读写的时候,我们要使该资源 ...

  5. HDU 4247 A Famous ICPC Team

    Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw ...

  6. spring security maven dependency

    Unable to locate Spring NamespaceHandler for XML schema namespace [ spring secutity dependency: < ...

  7. reshape2 数据操作 数据融合 (melt)

    前面一篇讲了cast,想必已经见识到了reshape2的强大,当然在使用cast时配合上melt这种强大的揉数据能力才能表现的淋漓尽致. 下面我们来看下,melt这个函数以及它的特点. melt(da ...

  8. Java 泛型 协变式覆盖和泛型重载

    Java 泛型 协变式覆盖和泛型重载 @author ixenos 1.协变式覆盖(Override) 在JDK 1.4及以前,子类方法如果要覆盖超类的某个方法,必须具有完全相同的方法签名,包括返回值 ...

  9. strstr() strpos() 获取db报错,判断报错中是否包含字符串,判断错误类型

    model中直接获取添加公司的错误.(公司名称不能重复) $enterprise_id = $this->add($enterprisedata ); $err = $this->getD ...

  10. 5、判断、循环、数组综合练习案例(迷你DVD)

    迷你dvd代码如下: package com.manager; import java.util.Scanner; public class DVDManage { public static voi ...