This script is a part of my workbench series and im hoping someone can find it useful. This will repair your messed up paths.or if someone in the forum is looking for help in the situation this will work 100% use with cautiion.
Step 1: Transfer
Copy the script name it "linux-doctor-offline.sh" and copy to a USB flash drive or physical drive mount to the offline target host.
Step 2: Permissions
Enable execute permissions in terminal:
chmod +x linux-doctor-offline.sh
Step 3: Diagnose
Execute targeted audits or complete health scan:
./linux-doctor-offline.sh --all
#!/usr/bin/env bash
==============================================================================
PATH DOCTOR - STANDALONE AIR-GAPPED LINTER & DEDUPLICATOR
==============================================================================
Resolves duplicate paths, trailing slashes, empty segments, and ordering conflicts.
Runs on any Linux or macOS machine with standard bash. No network needed.
Written by -Randy K from my tutorial on software engineering with Linux Work Bench.Tutorial.
Intermediate and Advanced Level Of Linux Coding For Engineers
Disclaimer use at your own Risk to fix your path structure. Use as a Last Resort.
==============================================================================
set -euo pipefail
CURRENT_PATH="${PATH:-}"
echo "Diagnosing current $PATH..."
echo "Original string length: ${#CURRENT_PATH} characters"
echo ""
IFS=':' read -r -a SEGMENTS <<< "$CURRENT_PATH"
SEEN=""
CLEAN_PARTS=()
DUPLICATES=()
INSECURE=()
for part in "${SEGMENTS[@]}"; do
Check for insecure empty or current directory (.) entries
if [ -z "$part" ] || [ "$part" = "." ]; then
INSECURE+=("EMPTY_OR_DOT")
continue
fi
Strip trailing slash
clean_part="${part%/}"
Check if already seen
if [[ ":$SEEN:" == ":$clean_part:" ]]; then
DUPLICATES+=("$clean_part")
else
SEEN="${SEEN}:${clean_part}"
CLEAN_PARTS+=("$clean_part")
fi
done
echo "--------------------------------------------------------"
echo "DIAGNOSTIC FINDINGS:"
echo "--------------------------------------------------------"
echo "Total Original Segments: ${#SEGMENTS[@]}"
echo "Duplicates Removed: ${#DUPLICATES[@]}"
if [ ${#DUPLICATES[@]} -gt 0 ]; then
for d in "${DUPLICATES[@]}"; do
echo " - Duplicate: $d"
done
fi
echo "Insecure Entries: ${#INSECURE[@]}"
if [ ${#INSECURE[@]} -gt 0 ]; then
echo " ! WARNING: Found empty segment or '.' in PATH. Vulnerable to binary hijacking."
fi
Build cleaned string
CLEANED_PATH=""
for cp in "${CLEAN_PARTS[@]}"; do
if [ -z "$CLEANED_PATH" ]; then
CLEANED_PATH="$cp"
else
CLEANED_PATH="${CLEANED_PATH}:${cp}"
fi
done
echo ""
echo "--------------------------------------------------------"
echo "CLEANED $PATH EXPORT:"
echo "--------------------------------------------------------"
echo "export PATH="$CLEANED_PATH""
echo ""
echo "To persist this change in your shell:"
echo " echo 'export PATH="$CLEANED_PATH"' >> ~/.bashrc"
echo " source ~/.bashrc"
This is the biggest request i get as many 10 times a week from student,teachers and leechers. my paths are screwd help me.
Good Luck