烈焰重击_重击历史的问题

2023-05-16

烈焰重击

There is — at least sometimes — a problem with history in bash and other Linux or Unix shells. If you are running more than one shell, they tend to fight over who controls the history. It isn’t uncommon to lose a previous command when one shell overwrites the history from another shell. Luckily, the problem is easy to fix.

bash和其他Linux或Unix shell的历史存在(至少有时是)问题。 如果您运行的shell不止一个,则它们会争夺控制历史的权限。 当一个shell覆盖另一个shell的历史记录时,丢失先前的命令并不罕见。 幸运的是,该问题很容易解决。

历史史 (History of History)

Once upon a time, Unix had a command prompt and it was good. But typing lines over and over again at 110 or 300 baud is a drag. So the shell introduced history. Using an arrow key or a control code or even a command you could select a previous command and run it again.

从前,Unix有一个命令提示符,这很好。 但是,以110或300波特的频率反复键入行是一种阻力。 因此,shell介绍了历史。 使用箭头键,控制代码甚至命令,您可以选择上一个命令并再次运行。

That was good, too. However, as time went on, people started running more than one shell at a time. Some used something like screen or, later, tmux. Others used X11. Nearly everyone switched to Linux, but you still had the same problem.

那也很好。 但是,随着时间的流逝,人们开始一次运行多个shell。 有些人使用了屏幕或后来的tmux之类的东西。 其他人使用X11。 几乎所有人都转而使用Linux,但是您仍然遇到相同的问题。

That problem is simple: you have one history file, and the shell reads it when it starts and writes it as it exits. So if shell A and shell B start at the same time, both will have the same history until you start issuing commands. You exit shell B and the history file now has the B copy. But when you exit shell A, you wipe out everything unique to B. When shell C starts, you only have the history from shell A.

这个问题很简单:您有一个历史文件,外壳程序在启动时会读取它,在退出时会将其写入。 因此,如果外壳程序A和外壳程序B同时启动,那么在您开始发出命令之前,它们将具有相同的历史记录。 您退出外壳B,历史记录文件现在具有B副本。 但是,当退出外壳A时,您将擦除B独有的所有内容。外壳C启动时,您只有外壳A的历史记录。

修复 (The Fix)

Luckily, there are a few ways around this. The first step is to make bash stop overwriting the history file, and append to it instead. This line in your .bashrc will do it:

幸运的是,有几种解决方法。 第一步是使bash停止覆盖历史记录文件,然后附加到它。 .bashrc中的这一行将执行此操作:

shopt -s histappend

This helps a little. Each shell will record its own history, but when it writes to the history file, you won’t lose other lines from other shells. Well, sort of. The variable HISTFILESIZE controls how many lines the file saves. Once you have that many lines, things start falling off, but that’s true even with a single shell.

这会有所帮助。 每个外壳程序都会记录自己的历史记录,但是当它写入历史记录文件时,您不会从其他外壳程序中丢失其他行。 好吧,有点。 变量HISTFILESIZE控制文件保存多少行。 一旦有那么多行,事情就会开始下降,但是即使只有一个外壳,也是如此。

Of course, your distribution may have already set that up for you. You can easily tell with this command:

当然,您的发行版可能已经为您设置了。 您可以使用以下命令轻松判断:

shopt | grep histappend

You should see something like this:

您应该会看到以下内容:

$ shopt | grep histappend
histappend off

Obviously, you want it to say “on” instead. If it already says on, you’ve already had half the work done for you.

显然,您希望它说“ on”。 如果已经说明,那么您已经为您完成了一半的工作。

更多修复 (More Fixes)

In some cases, that might be all you need to do. However, there are two history commands that you may want to use: history -a and history -n . The -a option causes the shell to write out any new history to the file. The -n option is sort of the opposite. It causes the shell to read any new lines.

在某些情况下,这可能就是您需要做的。 但是,您可能要使用两个history命令: history -ahistory -n 。 -a选项使外壳程序将所有新历史记录写到文件中。 -n选项与此相反。 它使外壳读取任何新行。

Of course, you don’t want to have to issue these commands every time you want to update the shell’s history, and you don’t have to. Simply set the PROMPT_COMMAND environment variable to cause these commands to execute at each prompt.

当然,您不需要每次都想更新外壳历史记录时都必须发出这些命令,也不必这样做。 只需设置PROMPT_COMMAND环境变量以使这些命令在每个提示符下执行。

警告! (Caution!)

There is one caveat. Other things may use the PROMPT_COMMAND variable to do other things, so it is a good idea to check to see if anything is already there and account for it when you make the change. So, for example:

有一个警告。 其他事情可能会使用PROMPT_COMMAND变量执行其他操作,因此,最好检查一下是否已有任何内容,并在进行更改时加以考虑。 因此,例如:

echo $PROMPT_COMMAND
# if nothing is there, great
# but maybe:
PROMPT_COMMAND="history -a ; history -n ; $PROMPT_COMMAND"

Of course, if you already see these commands in your PROMPT_COMMAND variable — or if there is a call to a script, function, or alias that makes the history calls — you don’t have to add them again. You’ll have to investigate a little to find out. If you do call it multiple times, it won’t hurt anything, but it will slow down your shell prompt a little.

当然,如果您已经在PROMPT_COMMAND变量中看到了这些命令-或者调用了进行历史记录调用的脚本,函数或别名,则无需再次添加它们。 您必须进行一些调查才能找到答案。 如果您多次调用它,它不会造成任何伤害,但是会稍微降低您的shell提示速度。

最大历史记录 (Maximum History)

There are many tricks to effectively use history, but making sure your history isn’t wiped out at random is a pretty important first step. Stay tuned for more about history, soon.

有效使用历史记录有很多技巧,但是确保您的历史记录不会被随机擦除是非常重要的第一步。 敬请期待有关历史的更多信息。

翻译自: https://medium.com/for-linux-users/the-problem-with-bash-history-260d69dd673b

烈焰重击

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

烈焰重击_重击历史的问题 的相关文章

随机推荐