免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2333 | 回复: 0
打印 上一主题 下一主题

bash中history实时写入带时间戳 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-14 14:33 |只看该作者 |倒序浏览
    在bash中,所执行的命令可以用history显示出来,但是bash的机制是:先将命令写入baffer,退出shell时才会写入.bash_history文件中,所以最后.bash_history文件中的命令无法分辨出来时间,解决这个问题可以采用时间戳的方法。
    bash中提供了时间戳的参数HISTTIMEFORMAT
               
               
                       HISTTIMEFORMAT
              If this variable is set and not null, its value is used  as  a  format string  for  strftime(3)  to print the time stamp associated with each history entry displayed by the history builtin.  If this  variable  is set,  time  stamps are written to the history file so they may be pre-served across shell sessions.
    有了这个参数,可以让执行的命令用history显示时,前面加上时间戳。举例如下:
1008  2009-10-14 14:31:00 history
1009  2009-10-14 14:32:23 cat .bash_profile
1010  2009-10-14 14:36:01 man bash
1011  2009-10-14 14:37:53 history
    另外就是需要让bash实时写入历史命令。这也有相关参数:
export PROMPT_COMMAND="history -a"         #用以实时写入history
export HISTTIMEFORMAT="%F %T "             #history带时间戳


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/43502/showart_2070198.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP