-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfilter_history.sh
executable file
·102 lines (87 loc) · 3.25 KB
/
filter_history.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh
########################################################################
# filter_history.sh: Remove specific entries from Zsh history file
#
# Description:
# This script removes lines containing a specified pattern from the Zsh
# history file. It creates a backup before modification and ensures
# safe execution with error handling.
#
# Features:
# - Creates a backup before modifying the history file.
# - Ensures TMP environment variable is set before proceeding.
# - Uses grep to filter out lines containing the specified pattern.
# - Provides error handling for file operations.
# - Displays the number of removed entries.
# - Checks for required commands before execution.
# - Displays a diff of the changes made to the history file.
#
# Author: id774 (More info: http://id774.net)
# Source Code: https://github.com/id774/scripts
# License: LGPLv3 (Details: https://www.gnu.org/licenses/lgpl-3.0.html)
# Contact: [email protected]
#
# Version History:
# v1.2 2025-03-16
# Encapsulated all logic in functions and introduced main function.
# v1.1 2025-03-13
# Redirected error messages to stderr for better logging and debugging.
# v1.0 2025-02-25
# Initial release with backup creation and error handling.
#
# Usage:
# ./filter_history.sh <pattern>
# <pattern>: The string to remove from the history file (partial match).
#
########################################################################
set -e # Exit immediately if a command exits with a non-zero status
# Function to check required commands
check_commands() {
for cmd in "$@"; do
cmd_path=$(command -v "$cmd" 2>/dev/null)
if [ -z "$cmd_path" ]; then
echo "Error: Command '$cmd' is not installed. Please install $cmd and try again." >&2
exit 127
elif [ ! -x "$cmd_path" ]; then
echo "Error: Command '$cmd' is not executable. Please check the permissions." >&2
exit 126
fi
done
}
# Main function to execute the script
main() {
# Required commands
check_commands grep cp mv basename diff
HISTORY_FILE="$HOME/.zsh_history"
# Check if TMP is defined
if [ -z "$TMP" ]; then
echo "Error: TMP environment variable is not set." >&2
exit 1
fi
BACKUP_FILE="$TMP/$(basename "$HISTORY_FILE").bak.$(date +%Y%m%d%H%M%S)"
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <string>"
exit 0
fi
PATTERN="$1"
# Create a backup of the history file
cp "$HISTORY_FILE" "$BACKUP_FILE"
echo "Backup created: $BACKUP_FILE"
# Count the number of matching lines
MATCH_COUNT=$(grep -c "$PATTERN" "$HISTORY_FILE")
# Remove lines containing the specified pattern
if ! grep -v "$PATTERN" "$HISTORY_FILE" > "$HISTORY_FILE.tmp"; then
echo "Error: Failed to create temporary file." >&2
exit 1
fi
# Overwrite the original history file
mv "$HISTORY_FILE.tmp" "$HISTORY_FILE"
echo "Lines containing '$PATTERN' have been removed from $HISTORY_FILE"
echo "Total removed entries: $MATCH_COUNT"
# Show the difference between the backup and the modified file
echo "Displaying changes:"
diff "$BACKUP_FILE" "$HISTORY_FILE" || true
}
# Execute main function
main "$@"