Skip to content

Instantly share code, notes, and snippets.

@cppcooper
Last active April 14, 2021 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cppcooper/473b332a0b54393b533b8f5135d29ef3 to your computer and use it in GitHub Desktop.
Save cppcooper/473b332a0b54393b533b8f5135d29ef3 to your computer and use it in GitHub Desktop.
removes regex pattern from bash history (also deduplicates history file)
#!/usr/bin/bash
if [ -z "$lastCMD" ]; then
if [ -z "$SHELLSTARTED" ]; then
export SHELLSTARTED="1";
else
echo -e "purgehistory_core: Error 1: \$lastCMD is empty";
fi
exit 1
elif [ ! -z "$HISTPURGEFAIL" ]; then
exit 2
fi
#phc_debug=1
lock="/tmp/.lock-historypurge"
exec 31>$lock
flock --timeout 5 31 || exit 1
HISTFILE="/"
# function to free lock and temp files
free_(){
rm -rf $lock
rm -rf $tmp_before
rm -rf $tmp_after
flock -u 31
exec 31>&-
}
# function for printing errors
error(){
case "$1" in
"2") echo -e "Error 2: purgehistory_core: Critical failue.\nCould not run pcregrep (probably). Will not try again this session."
export HISTPURGEFAIL="2"
;; "3") echo -e "purgehistory_core: Error 3: $bhistory is empty"
;; "4") echo -e "purgehistory_core: Error 4: failed: cp $bhistory $tmp_before\nPerhaps $tmp_before already exists."
;; "5") echo -e "purgehistory_core: Error 5: $tmp_before is empty"
;; "6") echo -e "purgehistory_core: Error 6: $tmp_after is empty"
;; *) echo "Not an error(yet), a typo" ;;
esac
free_
unset -f error
unset -f free_
return $(($1))
}
# function for checking if file is empty
isempty(){
if [[ -f $1 ]]; then
if [[ -s $1 ]]; then
if cat $1 2>/dev/null > /tmp/temp_file_prime ; then
return 1;
fi
fi
fi
return 0;
}
# the first argument given to this script is the regex to match the history against
if [ -z "$1" ]; then
core_regex="#\d{10}\s[^#\s].*"
else
core_regex="#\d{10}\s(?!$1)[^#\s].*"
fi
# debugging prints regex pattern
if [ ! -z "$phc_debug" ]; then
echo "debugging"
echo "$core_regex"
fi
# our files
bhistory="$HOME/.bash_history"
tmp_before="/tmp/.hist_before"
tmp_after="/tmp/.hist_after"
# if history is empty
if isempty $bhistory; then
error 3
exit 3
fi
# if cp operation fails
if cp $bhistory $tmp_before; then :
else
error 4
exit 4
fi
# if file copy is empty
if isempty $tmp_before; then
error 5
exit 5
fi
# deduplicate and remove history matching the user provided pattern
if cat $tmp_before | pcre2grep -M "$core_regex" | tac | awk '!seen[$0]++' | tac > $tmp_after; then
if [ ! -z "$phc_debug" ]; then :
# do nothing if debugging
elif isempty $tmp_after; then
# error if manipulated history is entirely empty
error 6
exit 6
else
# operation was successful, replace history and reload it
mv $tmp_after $bhistory
history -c
history -r
fi
else
# operation failed outright - assumed to be pcre2grep missing from system - error prints once only
error 2
exit 2
fi
# free up the lock, and undefine our internal functions
free_
unset -f error
unset -f free_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment