深入探索Shell编程:Here文档、自上而下设计、函数与流程控制
1. Here文档的介绍与使用
在输出文本时,除了使用echo命令,还有一种方法叫Here文档(here document)或Here脚本(here script)。Here文档是一种额外的I/O重定向形式,它允许我们将一段文本嵌入到脚本中,并将其作为命令的标准输入。其基本格式如下:
command << token text token其中,command是接受标准输入的命令名,token是用于指示嵌入文本结束的字符串。
下面是一个使用Here文档的脚本示例,用于输出系统信息页面:
#!/bin/bash # Program to output a system information page TITLE="System Information Report For $HOSTNAME" CURRENT_TIME="$(date +"%x %r %Z")" TIMESTAMP="Generated $CURRENT_TIME, by $USER" cat << _EOF_ <html> <head> <title>$TITLE</title> </head> <body