问题描述
我使用多个的bash会话,我想跟踪历史,从所有的人都在一个文件中(我不在乎它是由多个会话复用的,我可以随时把会话标识符在它的前面。 )我试图做
I use multiple bash sessions, and I want to keep track of history from all of them in one file (I don't care that it is multiplexed from multiple sessions, I can always put a session identifier in front of it.) I have tried doing
shopt -s histappend
和还加入
history -a
到 $ PROMPT_COMMAND
变量。但没有人真正为我工作,我不明白为什么他们不工作(他们的行为非常不确定性,据我可以告诉...有时他们从复多个会话的命令,有时他们不)。
to the $PROMPT_COMMAND
variable. But none of them really work for me, and I don't understand why they don't work (they behave very non-deterministically as far as I can tell... sometimes they multiplex commands from multiple sessions, sometimes they don't).
这个问题的目标是探索另一种方法来保持历史,从所有会话,在那里我可以控制我写的历史。我们的想法是存储previous命令,在一个shell变量,然后回显该变量的历史记录日志文件 PS1
变量的定义中。
The goal of this question is to explore an alternate way to keep history from all sessions, where I can control what I write to the history. The idea is to store "previous command" in a shell variable and then echo that variable to a history-log file inside the definition of PS1
variable.
现在的问题是:我得到一个shell变量previous执行命令怎么办。我知道我可以执行回音! >> LOGFILE.TXT
交互式bash命令将其记录到日志文件。但我怎么做到这一点的脚本文件(或.bashrc文件)?
The question is: How do I get the "previous executed command" in a shell variable. I know I can execute echo !! >> logfile.txt
in interactive bash session to record it to a log file. But how do I do this in a script file (or .bashrc file)?
我曾尝试
PROMPT_COMMAND="PC=$_;"
PREVIOUS_COMMAND=$(echo $PC) # $_ only gives the last argument of previous command
export PS1="[\u@\h \w] [$PREVIOUS_COMMAND $(echo $_) $_] $ "
但这些都不工作。
But none of this works.
感谢您的时间,
〜瑜伽士
Thanks for your time,~yogi
推荐答案
类似
fc -ln -1
应该工作。这就是说,你很可能运行到并发访问的问题(阅读:多壳覆盖彼此的历史),你可能无法用手做得更好
should work. That said, you're probably running into concurrent access issues (read: multiple shells overwriting each others' history) and you may not be able to do any better by hand.
这篇关于我如何获得" previous执行命令"在bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!