Bash脚本清除历史记录,关于更好的解决方案的问题

问题描述 投票:0回答:2

我正在尝试为Shell编写一个函数,以删除目录中的历史文件并删除不是当前正在运行的进程的文件。我遇到一个用例的问题:没有文件存在,xargs rm用空参数调用。

有人在不使用临时文件的情况下对处理这种空案件有建议吗?

谢谢

function purge_hist {
    for i in $(comm -23 <(sort -n <(ls $HOME/.hist/history.* | \
             sed "s#$HOME/.hist/history.##"))  < \
             (sort <(for i in $(ps -ex -o pid | sed 1d); 
                do 
                   echo $i ; 
                done)));
                do 
                   echo "$HOME/.hist/history.$i";
                done | xargs rm -f
}

更新解决方案有效

bash shell ksh
2个回答
1
投票

xargs rm -f是最简单的解决方案。


0
投票

@ rici的解决方案在rm的特定情况下会很好,但是如果您真的不想运行your_command(也可能是rm -f,而在xargs不存在的情况下)得到任何输入,在这种情况下您应该使用:

xargs --no-run-if-empty your_command
© www.soinside.com 2019 - 2024. All rights reserved.