I'll provide a comprehensive Bash script to check and validate your .bashrc configuration, system paths, and ensure compatibility for both root and user environments. Here's a detailed script:
#!/bin/bash
# Comprehensive .bashrc and System Path Validation Script
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to check file existence and permissions
check_file_status() {
local file="$1"
if [ -f "$file" ]; then
echo -e "${GREEN}✓ File exists:${NC} $file"
echo -e " Permissions: $(stat -c '%A' "$file")"
echo -e " Owner: $(stat -c '%U' "$file")"
else
echo -e "${RED}✗ File not found:${NC} $file"
fi
}
# Function to validate PATH entries
validate_path_entries() {
local user="$1"
echo -e "\n${YELLOW}Checking PATH for $user:${NC}"
# Get PATH for the specified user
local user_paths
if [ "$user" == "root" ]; then
user_paths=$(sudo -u root printenv PATH)
else
user_paths=$(printenv PATH)
fi
# Split and check each path
IFS=':' read -ra ADDR <<< "$user_paths"
for path in "${ADDR[@]}"; do
if [ -d "$path" ]; then
echo -e "${GREEN}✓ Valid Path:${NC} $path"
else
echo -e "${RED}✗ Invalid Path:${NC} $path"
fi
done
}
# Function to check .bashrc integrity
check_bashrc_integrity() {
local user="$1"
local bashrc_path
if [ "$user" == "root" ]; then
bashrc_path="/root/.bashrc"
else
bashrc_path="/home/$user/.bashrc"
fi
echo -e "\n${YELLOW}Checking .bashrc for $user:${NC}"
check_file_status "$bashrc_path"
# Basic syntax check
bash -n "$bashrc_path" 2>&1
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ Bash syntax check passed${NC}"
else
echo -e "${RED}✗ Syntax errors detected in .bashrc${NC}"
fi
# Check for common misconfigurations
grep -q "export PATH=" "$bashrc_path"
if [ $? -eq 0 ]; then
echo -e "${GREEN}✓ PATH export found${NC}"
else
echo -e "${YELLOW}⚠ No PATH export detected${NC}"
fi
}
# Function to validate system directories
validate_system_directories() {
local critical_dirs=(
"/usr/bin"
"/usr/sbin"
"/bin"
"/sbin"
"/etc"
"/home"
"/root"
)
echo -e "\n${YELLOW}Checking Critical System Directories:${NC}"
for dir in "${critical_dirs[@]}"; do
if [ -d "$dir" ]; then
echo -e "${GREEN}✓ Directory exists:${NC} $dir"
echo -e " Permissions: $(stat -c '%A' "$dir")"
else
echo -e "${RED}✗ Directory missing:${NC} $dir"
fi
done
}
# Main execution
main() {
echo -e "${YELLOW}Comprehensive System Configuration Check${NC}"
# Check for both users
users=("$(whoami)" "root")
for user in "${users[@]}"; do
validate_path_entries "$user"
check_bashrc_integrity "$user"
done
validate_system_directories
echo -e "\n${GREEN}Configuration Check Complete!${NC}"
}
# Run the main function
main
Usage Instructions:
- Save the script as
bashrc_check.sh
- Make it executable:
chmod +x bashrc_check.sh
- Run with:
./bashrc_check.sh
What This Script Does:
- Checks PATH validity for both users
- Validates
.bashrc file integrity
- Performs syntax checks on
.bashrc
- Verifies critical system directories
- Provides color-coded output for easy reading
Recommendations:
- If any issues are detected, review the specific paths or
.bashrc configurations
- Use
sudo when running to ensure complete system check
- Modify the
critical_dirs array if you need to check additional directories
Potential Fixes for Common Issues:
If the script reveals path or .bashrc problems, consider:
- Removing duplicate PATH entries
- Ensuring proper export statements
- Checking file permissions
- Verifying directory existence
Notes:
- This script provides a comprehensive overview
- It doesn't automatically fix issues, but highlights potential problems
- Always backup your configurations before making changes
Would you like me to elaborate on any part of the script or explain how to address specific types of configuration issues?