Here Document 是一种有特殊用处的代码块,他使用IO重定向的形式记录了一段临时的文本或交互命令,并且把这些文本或命令 依次的传递给一个程序或一个命令,作为他运行时的标准输入。

Here document的语法格式为

void@void-ThinkPad-E450:~/linuxShellArg$ command << delimiter
> document
> docuemnt
> document
> delimiter

bash把操作符<<看成输入指令,后面的delimiter是标识符开始和结束的标识符,bash会一直输出提示符>,用来等待用户输入数据,一直到用户输入第二个delimiter标识符为止,他表明输入结束。两个delimiter之间的数据都会作为command命令的标准输入。

标识符delimiter可以使用任意字符串,但是delimiter只能是一个词,中间不能有空格或Tab键。

Here document 经常o用在Shell脚本的帮助函数中,例如android的build/envsetup.sh

function hmm() {
cat <<EOF
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|armv5] [eng|userdebug|user]
- croot: Changes directory to the top of the tree.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
- mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
- cgrep: Greps on all local C/C++ files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- godir: Go to the directory containing a file. Look at the source to view more functions. The complete list is:
EOF
T=$(gettop)
local A
A=""
for i in `cat $T/build/envsetup.sh | sed -n "/^function /s/function \([a-z_]*\).*/\1/p" | sort`; do
A="$A $i"
done
echo $A
}

在函数hmm()中通过here document显示了一些帮助信息,这里通过cat命令 把here document输出到屏幕上,且delimiter标识符使用了EOF。

Linux Shell Here Document的更多相关文章

  1. linux shell的here document用法(cat << EOF)

    什么是Here Document?Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下cmd << delimiter  Here Docu ...

  2. linux shell的here document用法

    转载自: http://my.oschina.net/u/1032146/blog/146941 什么是Here Document?Here Document 是在Linux Shell 中的一种特殊 ...

  3. (转)linux shell 的here document 用法 (cat << EOF)

    什么是Here Documen: Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下 cmd << delimiter Here Docu ...

  4. linux shell编程总结

    linux shell编程总结 本周学习了unix/linux shell编程,参考的是<LINUX与UNIX Shell 编程指南>,David Tansley著:徐焱,张春萌等译,由机 ...

  5. linux shell基础语法

    1.第一个Shell脚本 打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好,如果你用php写shell 脚本,扩展名就用php好了. 输入一些代码 ...

  6. Linux shell下30个有趣的命令

    Tips 原文作者:Víctor López Ferrando 原文地址:30 interesting commands for the Linux shell 这些是我收集了多年的Linux she ...

  7. (转) Linux Shell经典实例解析

    原文:http://blog.csdn.net/yonggeit/article/details/72779955 该篇博客作为对之前Linux Shell常用技巧和高级技巧系列博客的总结,将以Ora ...

  8. Linux shell之重定向输入,输出

    shell是一个命令解释器,它在操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种各样的操作系统的输出结果,输出到屏幕返回给用户.这种对话方式可以是交互的方式(从键盘输入命令 ...

  9. Linux shell脚本基础学习详细介绍(完整版)二

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

随机推荐

  1. docker 搭建 zipkin

    1.拉镜像 docker pull openzipkin/zipkin 2.运行镜像 docker run -d --restart always -p 9411:9411 --name zipkin ...

  2. BZOJ_1008 越狱(快速幂)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1008 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教 ...

  3. UI自动化测试:App的Webview页面元素左滑

      一.前言 在做App自动化测试时,我们会遇到如上图所示的列表数据页面左滑删除场景,一般可以通过location.rect方法获取对应列表的元素坐标,然后使用TouchAction或者swipe滑动 ...

  4. ecshop调用指定栏目下的商品的方法

    第一步 在系统目录文件找到includes/lib_goods.php  这个文件打开 在此页最底部加入以下函数代码 /** * 首页获取指定分类产品 * * @access public * @pa ...

  5. SQL-关联查询【转】

    T_A A表 T_B B标,id为表与表相关联的字段`创建相关表结构 CREATE TABLE Table_B( id INT(2), serNum VARCHAR(10) ); CREATE TAB ...

  6. 定要过python二级选择题二套

    1. 2. 3. 4. 5. 6. 7. 8. 非主属性???? 9. 10.. 11.` 调用只是调用,但是没有返回值,,应为里面没有输出 12. 13. 14. 15. 16. 18. 19.

  7. 定要过python二级 第一套

    1.和强类型语言相对应的是弱类型语言,Python.JavaScript.PHP 等脚本语言一般都是弱类型的.弱类型语言有两个特点: 变量无须声明就可以直接赋值,对一个不存在的变量赋值就相当于定义了一 ...

  8. P4716-[模板]最小树形图

    正题 题目链接:https://www.luogu.com.cn/problem/P4716 题目大意 给出\(n\)个点\(m\)条边的一张有向图,求以\(r\)为根的最小外向树. \(1\leq ...

  9. P3308-[SDOI2014]LIS【最小割】

    正题 题目链接:https://www.luogu.com.cn/problem/P3308 题目大意 三个\(n\)个数字的序列\(A,B,C\).要求删除其中某些位置\(i\)使得\(A\)的最长 ...

  10. Filter和Interceptor

    Filter的使用 拦截user下的所有请求 @WebFilter("/user/*") public class UserFilter implements Filter { @ ...