Here document
来源:互联网 发布:linux流量统计分析 编辑:程序博客网 时间:2024/05/22 13:08
Here document
Here document (here-document, here-text, heredoc, hereis, here-string, here-script)是一个文件文字或输入流文字:它是源代码文件的一部分,被视为它是一个单独的文件。该术语还用于使用类似语法,保留换行符和文本中的其他空格(包括缩进)的多行字符串文字形式。
简单的说here document将内容I/O重定向到交互程序或终端,在编写脚本时十分方便。
在Unix Shell中使用Here document
使用示例
命令 << EOF //EOF作为定界符可以是任何字符串,结束时要使用对应的字符进行结束...EOF //这里结束符前面不能有空格、缩进,大小写也要保持一致
当需要输入一大段内容到某个文件,使用echo输入一段大内容就显得太麻烦了。
echo "one" > outfile.txtecho "two" >> outfile.txtecho -e "three\nfour" >> outfile.txt
Here document可以更加简单明了的进行同样的操作。
cat << EOF > outfile.txtonetwothreefourEOF
Here document与交互程序进行操作
mysql -u root << SQLexitSQL
忽略缩进
在“<<”后面加上“-”符号可以忽略缩进
cat <<- EOF one two threeEOF
输出结果:
one
two
three
对比不加“-”符号输出结果:
cat << EOF one two threeEOF
输出结果:
one
two
three
使用’EOF’、”EOF”或`EOF`
当定界符被任何引号括住,将不会对内容进行处理。
示例:
xyz=Hellocat << EOF `date` $xyzEOF
输出结果:
2017年 7月20日 星期四 12时28分55秒 CST
Hello
当我们不希望,”$xyz”和”`date`”被处理时,又不希望使用”\”进行字符转义 影响可读性,
可以使用引号将定界符括住。
xyz=Hellocat << 'EOF' `date` $xyzEOF
输出结果:
`date`
$xyz
xyz=Hellocat << "EOF" `date` $xyzEOF
输出结果:
`date`
$xyz
还可以使用反引号,不过结束时也要带上 ` 符号
xyz=Hellocat << `EOF` `date` $xyz`EOF`
输出结果:
`date`
$xyz
在PHP中使用Here document
例如:
<?phpyour_name = "Cola";echo <<<EOFhello, $your_name!testEOF;hi = <<<EOFHello,world!EOF;echo $hi;?>
输出:
hello,Cloa!testHello,world!
阅读全文
0 0
- Here Document
- Here Document
- Here document
- here document
- here document
- Here Document
- Here-Document
- Here document
- vim && here document
- Here Document (二)
- Shell之Here Document
- Shell之Here Document
- perl here document
- here-document技术
- Shell之Here Document
- 有用的 here Document
- Shell之Here Document
- 什么是Here Document
- insufficient permission for adding an object to repository database
- css复习——overflow
- Python编写csdn刷博客数量软件
- python 分析监控日志
- Mac安装mysqldb
- Here document
- AngularJS实现增删改查带路由的
- Angularjs框架集成pagination
- 观察者设计模式
- 设计一个名为Circle2D的类
- STM32之内部3个ADC+2个DMA同时采集14路信号
- usaco6.1.2 A Rectangular Barn
- 65个面试常见问题技巧回答
- Java OOP