-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-alias.sh
More file actions
executable file
·69 lines (57 loc) · 2.01 KB
/
setup-alias.sh
File metadata and controls
executable file
·69 lines (57 loc) · 2.01 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
#!/bin/bash
# setup-alias.sh - Sets up shell aliases for WebDev Backup Tool
# Set restrictive umask to ensure secure file creation
umask 027
# Get the script's directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Function to sanitize paths against command injection
sanitize_path() {
local path="$1"
# Remove potential command injection characters
echo "$path" | tr -d ';&|$()`'
}
MAIN_SCRIPT="$(sanitize_path "$SCRIPT_DIR/webdev-backup.sh")"
# Banner
echo -e "\033[0;36m===== WebDev Backup Tool Alias Setup =====\033[0m"
echo "This script will add an alias to your ~/.zshrc file"
echo "The alias 'webback' will run the WebDev Backup Tool from any directory"
echo ""
# Check if script exists
if [ ! -f "$MAIN_SCRIPT" ]; then
echo -e "\033[0;31mERROR: Main script not found at $MAIN_SCRIPT\033[0m"
exit 1
fi
# Make sure the script is executable
chmod +x "$MAIN_SCRIPT"
# Create alias line
ALIAS_LINE="alias webback='$MAIN_SCRIPT'"
# Check if ~/.zshrc exists
if [ ! -f ~/.zshrc ]; then
echo -e "\033[0;33mCreating ~/.zshrc file\033[0m"
touch ~/.zshrc
fi
# Check if alias already exists
if grep -q "alias webback=" ~/.zshrc; then
echo -e "\033[0;33mAlias already exists in ~/.zshrc\033[0m"
# Update existing alias - escape the replacement string to prevent command injection
escaped_alias_line=$(printf "%s" "$ALIAS_LINE" | sed 's/[\/&]/\\&/g')
sed -i "s|alias webback=.*|$escaped_alias_line|" ~/.zshrc
echo -e "\033[0;32mUpdated existing alias to point to: $MAIN_SCRIPT\033[0m"
else
# Add the alias to ~/.zshrc
echo "" >> ~/.zshrc
echo "# WebDev Backup Tool alias" >> ~/.zshrc
echo "$ALIAS_LINE" >> ~/.zshrc
echo -e "\033[0;32mAdded alias to ~/.zshrc successfully\033[0m"
fi
# Remind the user to restart the shell or source the file
echo ""
echo "To use the alias immediately, run:"
echo " source ~/.zshrc"
echo ""
echo "You can now run the WebDev Backup Tool from any directory using:"
echo " webback"
echo ""
# Make this script executable
chmod +x "$0"
exit 0