Vij*_*ran 21 scripting bash command-history
History 是一个 shell 内置命令,我无法在 BASH 脚本中使用它。那么,有没有办法使用 BASH 脚本来实现这一点?
在这里,我们为您准备了我的脚本:
#!/bin/bash
history | tail -100 > /tmp/history.log
cd /tmp
uuencode history.log history.txt | mail -s "History log of server" hello@hel.com
Run Code Online (Sandbox Code Playgroud)
Gil*_*il' 35
默认情况下,Bash 在非交互式 shell 中禁用历史记录,但您可以将其打开。
#!/bin/bash
HISTFILE=~/.bash_history
set -o history
history | tail …
Run Code Online (Sandbox Code Playgroud)
但是,如果您试图监视该服务器上的活动,则 shell 历史记录是无用的(运行未显示在历史记录中的命令是微不足道的)。请参阅如何在 Linux 中记录所有进程启动。
如果您正在调试脚本,那么 shell 历史记录并不是获取有用信息的最佳方式。一个更好的工具是调试跟踪工具:放在set -x脚本顶部附近。跟踪被写入标准错误。
我不确定它在非交互式运行时是否真的使用了历史功能,否则您运行的每个 shell 脚本都会使您的命令历史变得混乱。
为什么不直接去源码${HOME}/.bash_history,替换history | tail -100为tail -100 ${HOME}/.bash_history. (如果您使用时间戳,您可能必须按照 的方式做一些事情grep -v ^# ${HOME}/.bash_history | tail -100)。
| 归档时间: |
|
| 查看次数: |
33997 次 |
| 最近记录: |