创建回显时间戳/不能在回显字符串中使用管道
我想使用时间戳来记录脚本文件中的输出。
在控制台上
date +%Y%m%d_%H%M%S_%N
打印类似 "20120206_104531_944652200" 的内容,
以便剪切所有这些 nano 和 mikro 秒
我找到了日期 +%Y%m%d_%H%M%S_%N | sed s,".\{6\}$",,
这给了我类似 "20120206_104531_944"
在脚本文件中它似乎有些不同。 每次完成日志输出时都必须评估时间戳。
此脚本代码有效:
#!/usr/bin/bash
log_prefix1="date +%Y%m%d_%H%M%S_%N"
echo "`${log_prefix1}` blabla"
sleep 3
echo "`${log_prefix1}` blabla"
不幸的是,我没有找到一种方法来仅使用纳秒的 3 个最高有效数字来运行它。
此脚本代码不起作用:
#!/usr/bin/bash
log_prefix="date +%Y%m%d_%H%M%S_%N | sed s,\".\{6\}$\",,"
echo "`${log_prefix}` blabla"
sleep 3
echo "`${log_prefix}` blabla"
我收到以下错误消息:
date: extra operand `|'
Try `date --help' for more information.
因此,执行时 sed 的管道出现问题。
有什么想法吗?
I would like to use timestamps for logging outputs in a scriptfile.
On the console
date +%Y%m%d_%H%M%S_%N
prints something like "20120206_104531_944652200"
In order to cut all those nano and mikro seconds I found
date +%Y%m%d_%H%M%S_%N | sed s,".\{6\}$",,
which gives me something like "20120206_104531_944"
In a scriptfile it seems to be somewhat different.
The timestamp must be evaluated each time a logging output is done.
This scriptcode works:
#!/usr/bin/bash
log_prefix1="date +%Y%m%d_%H%M%S_%N"
echo "`${log_prefix1}` blabla"
sleep 3
echo "`${log_prefix1}` blabla"
Unfortunately I did not find a way to get this running with only the 3 most significant digits of the nanoseconds.
This scriptcode does not work:
#!/usr/bin/bash
log_prefix="date +%Y%m%d_%H%M%S_%N | sed s,\".\{6\}$\",,"
echo "`${log_prefix}` blabla"
sleep 3
echo "`${log_prefix}` blabla"
I get the following error message:
date: extra operand `|'
Try `date --help' for more information.
So, something is going wrong with the pipe to sed at execution time.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过为此创建一个函数来让您的生活变得更简单。
像这样的东西:
You can make your life a bit simpler by creating a function for that.
Something like: