Shell—— 11.cat和重定向

本文详细介绍了在Shell脚本中如何使用heredoc语法来输入多行文本到文件,包括如何覆盖或追加内容,以及heredoc结束符的使用规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[root@master tmp]# cat <<eof>log.txt   # 覆盖的方式输入到log.txt
>  ..........
> eof

[root@master tmp]# cat >log1.txt <<eof 
> ..........
> eof

eof部分都必须使用"<<eof",它表示here document,此后输入的内容都作为一个document输入给cat。既然是document,那就肯定有document结束符标记document到此结束,结束符使用的是here document后的字符,例如此处为eof。其实不使用eof,使用其他字符也是一样的,但document的结束符也必须要随之改变。

[root@master ~]# cat <<abcx
> 123
> 345
> abcx
123
345

另一方面,>log1.txt表示将document的内容覆盖到log1.txt文件中,如果是要追加,则使用>>log1.txt。

[root@master tmp]# cat >>log1.txt <<eof 
> this is stdin character first!
> eof

[root@master tmp]# cat <<eof>>log1.txt 
> this is stdin character first!
> eof

————Blueicex 2020/03/07 21:40 blueice1980@126.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值