在Bash脚本中串联文本和命令输出以输出到文件 [英] Concatenating text and command output in Bash script to output to file

查看:71
本文介绍了在Bash脚本中串联文本和命令输出以输出到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在FreeBSD中编写一个bash脚本,该脚本将命令附加到日志文件.在执行将追加日志数据的命令之前,我想在文件中打印一行,以在数据上方显示当前日期,如下所示:

I'm writing a bash script in FreeBSD that appends commands to a log file. Before I execute the commands that will append the log data, I want to print a line in the file which shows the current date above the data, like this:

--- 2019年8月20日星期二17:26:37 ---

---Tue Aug 20 17:26:37 EDT 2019---

我知道我可以使用date命令输出时间戳,但是我不确定如何在时间戳之前和之后添加"---"以添加到文件中.最简单的方法是什么?

I know that I can use the date command to output the timestamp, but I'm not sure how to include the "---" before and after the timestamp to add to the file. What's the simplest way to do this?

推荐答案

您可以将格式字符串传递给 日期 :

You can pass a format string to date:

date '+---%a %b %e %H:%M:%S %Z %Y---'

这篇关于在Bash脚本中串联文本和命令输出以输出到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆