2013年3月20日 星期三 晴



Here Document



1、命令<<标记


输入内容


标记



[root@desktop7 ~]# cat <<EOT


> cengjing


> youyiduan


> zhenshi de gushi


> EOF


> EOT


cengjing


youyiduan


zhenshi de gushi


EOF


[root@desktop7 ~]#



2、统计文件(输入内容)行数



[root@desktop7 ~]# wc -l<<Here


> Hello


> the,wirld


> Wellcome to Chinese


> Here


3


[root@desktop7 ~]#



3、统计



[root@desktop7 ~]# wc -l


jin tian tianqi


hen qing lang


chu chu


hao fengguan


niao er mang feng er ye mang                #只有当Ctrl+d组合键才会终止文件的输入


5


[root@desktop7 ~]#



4、支持变量



[root@desktop7 ~]# A=7758521


[root@desktop7 ~]# cat <<END


> bu yao wen wo wei shen me


> cong qian mei youxun zhao


> girl friend


> yin wei


> wo de jian bang


> hai bu zu yi wei ta zhe feng dang yu


> qi shi wo ye xiang shuo


> $A


> END


bu yao wen wo wei shen me


cong qian mei youxun zhao


girl friend


yin wei


wo de jian bang


hai bu zu yi wei ta zhe feng dang yu


qi shi wo ye xiang shuo


7758521


[root@desktop7 ~]#



5、屏蔽特殊字符



[root@desktop7 ~]# cat <<'END'


> bu yao wen wo wei shen me


> cong qian mei youxun zhao


> girl friend


> yin wei


> wo de jian bang


> hai bu zu yi wei ta zhe feng dang yu


> qi shi wo ye xiang shuo


> $A


> EBD


> END


bu yao wen wo wei shen me


cong qian mei youxun zhao


girl friend


yin wei


wo de jian bang


hai bu zu yi wei ta zhe feng dang yu


qi shi wo ye xiang shuo


$A                                                 #特殊字符$A并未取变量值


EBD


[root@desktop7 ~]#



6、取变量值



[root@desktop7 ~]# m=$(cat <<EOF


> HELLO


> how are you


> fine


> thank you


> and you


> EOF)


[root@desktop7 ~]# echo $m


HELLO how are you fine thank you and you


[root@desktop7 ~]#



7、多行批注



编辑文本中可以用":<<END",然后在下面文本中输入“END“,则这之间的内容为多行批注,BASH不会执行。作用等同于加(#)井号,但是许多脚本中并未应用这种方式,仍旧多为井号。