-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreset-bash-profile.sh
More file actions
124 lines (104 loc) · 3.6 KB
/
Copy pathreset-bash-profile.sh
File metadata and controls
124 lines (104 loc) · 3.6 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# reset-bash-profile.sh - Script to reset bash profile to Debian 12 defaults
# Usage: sudo ./reset-bash-profile.sh [username]
# Author: Luis Miguel P. Freitas
# Contact me at: luis@digitalxs.ca or https://digitalxs.ca
# YOU ARE ABSOLUTELY FREE TO USE THIS SCRIPT AS YOU WANT. IT'S YOUR RESPONSABILITY ALONE!
# Function to check if script is run as root
check_root() {
if [ "$EUID" -ne 0 ]; then
echo "Error: Please run as root (use sudo)"
exit 1
fi
}
# Function to validate username
validate_user() {
local username=$1
if ! id "$username" >/dev/null 2>&1; then
echo "Error: User '$username' does not exist"
exit 1
fi
}
# Function to get user home directory
get_home_dir() {
local username=$1
echo "$(getent passwd "$username" | cut -d: -f6)"
}
# Main script
main() {
# Check if username is provided
if [ $# -ne 1 ]; then
echo "Usage: $0 [username]"
echo "Example: $0 johndoe"
exit 1
fi
# Store username
local username=$1
# Check root privileges
check_root
# Validate username
validate_user "$username"
# Get user's home directory
local home_dir
home_dir=$(get_home_dir "$username")
echo "Resetting bash profile for user: $username"
echo "Home directory: $home_dir"
# Create backup directory
local backup_dir
backup_dir="$home_dir/bash_backup_$(date +%Y%m%d_%H%M%S)"
echo "Creating backup directory: $backup_dir"
mkdir -p "$backup_dir"
# Verify that default files exist in /etc/skel/
for file in .bashrc .profile .bash_logout; do
if [ ! -f "/etc/skel/$file" ]; then
echo "Warning: Default file /etc/skel/$file not found"
fi
done
# Backup existing files
echo "Backing up existing files..."
for file in .bashrc .profile .bash_logout; do
if [ -f "$home_dir/$file" ]; then
cp "$home_dir/$file" "$backup_dir/" 2>/dev/null || echo "Warning: Could not backup $file"
fi
done
# Backup additional bash-related files
additional_files=".bash_aliases .bash_history .bashrc_help .config/starship.toml"
for file in $additional_files; do
if [ -f "$home_dir/$file" ]; then
echo "Found additional file: $file (backing up)"
# Create directory structure if needed
mkdir -p "$backup_dir/$(dirname "$file")" 2>/dev/null
cp "$home_dir/$file" "$backup_dir/$file" 2>/dev/null || echo "Warning: Could not backup $file"
fi
done
# Check for and remove symlinks
for file in .bashrc .profile .bash_logout .bashrc_help; do
if [ -L "$home_dir/$file" ]; then
echo "Removing symlink: $file"
rm "$home_dir/$file"
fi
done
# Copy default files
echo "Copying default files from /etc/skel/..."
for file in .bashrc .profile .bash_logout; do
cp "/etc/skel/$file" "$home_dir/" 2>/dev/null || echo "Warning: Could not copy $file"
done
# Set permissions
echo "Setting correct permissions..."
for file in .bashrc .profile .bash_logout; do
chown "$username:$username" "$home_dir/$file"
chmod 644 "$home_dir/$file"
done
echo "
Bash profile reset completed successfully!
- Backup files are stored in: $backup_dir
- Default files have been restored
- Permissions have been set
- Symlinks have been removed
To apply changes, the user should either:
1. Log out and log back in
2. Run: source ~/.bashrc
Note: If you had custom modifications, check the backup files to restore them manually."
}
# Run main script
main "$@"