将时间戳记添加到shell脚本的cat输出中 [英] Add timestamp to cat output from shell script

查看:179
本文介绍了将时间戳记添加到shell脚本的cat输出中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的脚本,可以将ttyUSB的输出记录到一个文件中,我想为每行加上一个时间戳.从命令行可以完成我想要的一切:

I have a small script that cats the output from the ttyUSB to a file I would like to prepend a timestamp to each line. From the command line this does everything I want:

$ cat /dev/ttyUSB0 /home/pi/daily_logs/ttyUSSB0 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }

我的问题是,当我将其添加到脚本中时,所有内容都可以正常工作,但未添加awk时间戳.我的脚本行如下所示:

My issue is that when I add it to a script everything works but the awk timestamp isn't added. My script line looks like this:

cat < /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; } &

任何帮助实现这一目标的方法,将不胜感激.

Any help getting this going would be appreciated.

推荐答案

您需要将awk的输出重定向到文件,而不是cat的文件.有了它,awk一无所获.实际上,您可能根本不需要猫:

You need to redirect awk's output to the file, not cat's. The way you have it, awk gets nothing. Actually, you may not need cat at all:

awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; }' /dev/ttyUSB0 > /home/pi/daily_logs/ttyUSB0 &

这篇关于将时间戳记添加到shell脚本的cat输出中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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