在 bash MacOSX 中创建不包括某些命令的日志历史记录
Create log history excluding some command in bash MacOSX
无法找到答案:
我想 自动 创建我的命令行日志历史记录,而无需执行任何操作。
为此我找到了一些线索,我修改了我的 .bash_profile
但我需要在我的日志中排除一些我不想要的命令,比如 "ls, cd, etc."
这行不通,我不能 d
所以这是我的代码:
# log every command typed and when
command_out=( "more" "less" "cd" "open" "ls" "pwd" "nano" "man" "help") #array of command i don't want to save in my log
my_TEST=0 ##setup a var
FIRST_COMMAND=$(echo $BASH_COMMAND| cut -d' ' -f 1) ##get only the first command
## test if the first command is in the array
for elm in "${command_out[@]}"; do
if [[ $FIRST_COMMAND == $elm ]]; then
echo $elm # does not work
$my_TEST=1 ## if the command is in the array the var is setup to 1
fi
done
if [[ $my_TEST == 0 ]] && [ -n "${BASH_VERSION}" ]; then
trap "caller >/dev/null || \
printf '%s\n' \"$(date '+%Y-%m-%dT%H:%M:%S%z')\
$(tty) ${BASH_COMMAND}\" 2>/dev/null >>~/.command_log" DEBUG
fi
如果你有任何其他关于如何做我想做的事情的想法,我很开放
谢谢
Bash 自动保留您键入的每个命令的历史记录;可以使用history
命令查看。如果你想排除某些命令,而不是试图从日志中排除它们,我会在查看它时跳过它们,例如history | egrep -vw 'ls|cd|other|commands|here'
。
您可以设置 HISTTIMEFORMAT
以获取每个条目的时间戳,控制使用 HISTFILESIZE
保留多少命令,如果您真的想将某些命令保留在外而不是看不到它们当你看的时候,你可以在 HISTIGNORE
中列出它们。参见 https://www.gnu.org/software/bash/manual/html_node/Using-History-Interactively.html。
您的 ~/.bash_history
文件应该已经包含您完整的命令历史记录。你可以使用类似的东西
cat ~/.bash_history | grep -v cd | egrep -v 'cd|ls|...'
过滤掉您不感兴趣的命令。
因此对于您指定的列表:
cat ~/.bash_history | egrep -v 'more|less|cd|open|ls|pwd|nano|man|help'
我用我想要的东西完成了 Mark Reed 的回答。这是我的代码:
# changes the .bash_history file mode to append
shopt -s histappend
#configures the history -a command to be run at each shell prompt. The -a immediately writes the current/new lines to the history file.
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
#list of command i don't want in my history
HISTIGNORE='ls*:exit:pwd:clear:cd*:man*:more*:less*:head*:tail*:nano*:open*:help*'
#set no limit to the history file size
HISTSIZE= HISTFILESIZE=
无法找到答案: 我想 自动 创建我的命令行日志历史记录,而无需执行任何操作。
为此我找到了一些线索,我修改了我的 .bash_profile 但我需要在我的日志中排除一些我不想要的命令,比如 "ls, cd, etc." 这行不通,我不能 d
所以这是我的代码:
# log every command typed and when
command_out=( "more" "less" "cd" "open" "ls" "pwd" "nano" "man" "help") #array of command i don't want to save in my log
my_TEST=0 ##setup a var
FIRST_COMMAND=$(echo $BASH_COMMAND| cut -d' ' -f 1) ##get only the first command
## test if the first command is in the array
for elm in "${command_out[@]}"; do
if [[ $FIRST_COMMAND == $elm ]]; then
echo $elm # does not work
$my_TEST=1 ## if the command is in the array the var is setup to 1
fi
done
if [[ $my_TEST == 0 ]] && [ -n "${BASH_VERSION}" ]; then
trap "caller >/dev/null || \
printf '%s\n' \"$(date '+%Y-%m-%dT%H:%M:%S%z')\
$(tty) ${BASH_COMMAND}\" 2>/dev/null >>~/.command_log" DEBUG
fi
如果你有任何其他关于如何做我想做的事情的想法,我很开放
谢谢
Bash 自动保留您键入的每个命令的历史记录;可以使用history
命令查看。如果你想排除某些命令,而不是试图从日志中排除它们,我会在查看它时跳过它们,例如history | egrep -vw 'ls|cd|other|commands|here'
。
您可以设置 HISTTIMEFORMAT
以获取每个条目的时间戳,控制使用 HISTFILESIZE
保留多少命令,如果您真的想将某些命令保留在外而不是看不到它们当你看的时候,你可以在 HISTIGNORE
中列出它们。参见 https://www.gnu.org/software/bash/manual/html_node/Using-History-Interactively.html。
您的 ~/.bash_history
文件应该已经包含您完整的命令历史记录。你可以使用类似的东西
cat ~/.bash_history | grep -v cd | egrep -v 'cd|ls|...'
过滤掉您不感兴趣的命令。
因此对于您指定的列表:
cat ~/.bash_history | egrep -v 'more|less|cd|open|ls|pwd|nano|man|help'
我用我想要的东西完成了 Mark Reed 的回答。这是我的代码:
# changes the .bash_history file mode to append
shopt -s histappend
#configures the history -a command to be run at each shell prompt. The -a immediately writes the current/new lines to the history file.
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
#list of command i don't want in my history
HISTIGNORE='ls*:exit:pwd:clear:cd*:man*:more*:less*:head*:tail*:nano*:open*:help*'
#set no limit to the history file size
HISTSIZE= HISTFILESIZE=