【shell实战之】使用临时文件_记录消息

这篇文章展示了如何在Bash脚本中利用tee命令来创建临时文件并记录消息。tee命令可以将数据同时输出到标准输出和指定文件,通过选项-a追加内容而不是覆盖原有信息。文中给出了一个示例,显示了如何分三次向同一个临时文件追加不同行的信息,并最终通过cat命令展示文件内容。
摘要由CSDN通过智能技术生成

使用临时文件_记录消息

#!/bin/bash
# tee命令:像是链接管道的T型接头,将来自STDIN的数据同时送往两处。
# 一处是STDOUT,另一处是tee命令所指定的文件名。
# -a选项:追加内容,不带该选项,会覆盖。

# using the tee command for logging
tempfile=test.v
echo "this is the start of the test" | tee $tempfile
echo "this is the second line of the test" | tee -a $tempfile
echo "this is the end of the test" | tee -a $tempfile

echo "*********** cat $tempfile ***********"
cat $tempfile

运行结果

this is the start of the test
this is the second line of the test
this is the end of the test
*********** cat test.v ***********
this is the start of the test
this is the second line of the test
this is the end of the test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值