我刚刚在bash中运行以下代码:
uniq .bash_history > .bash_history
我的历史档案最终完全空了.
我想在写入之前我需要一种方法来读取整个文件.
怎么做的?
PS:我显然想过使用临时文件,但我正在寻找更优雅的解决方案.
解决方法
我建议使用
moreutils的海绵.从联机帮助页:
DESCRIPTION sponge reads standard input and writes it out to the specified file. Unlike a shell redirect,sponge soaks up all its input before opening the output file. This allows for constructing pipelines that read from and write to the same file.
要将此问题应用于您的问题,请尝试:
uniq .bash_history | sponge .bash_history