[root@xuexi tmp]# cat <<eof>log.txt # 覆盖的方式输入到log.txt > this is stdin character > eof
也可以使用下面的方法。
[root@xuexi tmp]# cat >log1.txt <<eof > this is stdin character first! > eof
一方面,eof部分都必须使用"<<eof",它表示here document,此后输入的内容都作为一个document输入给cat。既然是document,那就肯定有document结束符标记document到此结束,结束符使用的是here document后的字符,例如此处为eof。其实不使用eof,使用其他字符也是一样的,但document的结束符也必须要随之改变。如:
[root@xuexi ~]# cat <<abcx > 123 > 345 > abcx 123 345
另一方面,>log1.txt表示将document的内容覆盖到log1.txt文件中,如果是要追加,则使用>>log1.txt。所以,追加的方式如下:
[root@xuexi tmp]# cat >>log1.txt <<eof > this is stdin character first! > eof
或
[root@xuexi tmp]# cat <<eof>>log1.txt > this is stdin character first! > eof
1.8.2.3 tee双重定向
可以使用tee双重定向。一般情况下,重定向要么将信息输入到文件中,要么输出到屏幕上,但是既想输出到屏幕又想输出到文件就比较麻烦。使用tee的双重定向功能可以实现该想法。如图。
tee [-a] file
选项说明:
-a:默认是将输出覆盖到文件中,使用该选项将变为追加行为。
file:除了输出到标准输出中,还将输出到file中。如果file为"-",则表示再输入一次到标准输出中。
例如下面的代码,将a开头的文件内容全部保存到b.log,同时把副本交给后面的的cat,使用这个cat又将内容保存到了x.log。其中"-"代表前面的stdin。
[root@xuexi tmp]# cat a* | tee b.log | cat - >x.log
还可以直接输出到屏幕:
[root@xuexi tmp]# cat a* | tee b.log | cat
tee默认会使用覆盖的方式保存到文件,可以使用-a选项来追加到文件。如:
[root@xuexi tmp]# cat a* | tee -a b.log | cat
现在就可以在使用cat和重定向创建文件或写入内容到文件的同时又可以在屏幕上显示一份。
[root@xuexi tmp]# cat <<eof | tee ttt.txt > x y > z 1 > eof x y z 1
1.8.2.4 <<和<<<
在bash中,<<和<<<是特殊重定向符号。<<表示的是here document,<<<表示的是here string。
here document在上文已经解释过了,对于here string,表示将<<<后的字符串作为输入数据。
例如:
passwd --stdin user <<< password_value










