Created
December 8, 2012 22:25
-
-
Save erluko/4242255 to your computer and use it in GitHub Desktop.
Fix the mess that /usr/libexec/path_helper made of my $PATH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#Reorders $PATH, removes duplicates | |
prefixes="$1" | |
if [ '--' = "$prefixes" ]; then | |
shift; | |
prefixes="$1"; | |
else | |
if echo "$prefixes" | grep -q '^\(\|--help\|-h\)$' ; then | |
echo "USAGE: pathfix.sh [--] PREFIXLIST [PATHLIST]" | |
echo "Arguments:" | |
echo " PREFIXLIST a colon separated list of path prefixes, required" | |
echo " PATHLIST a colon separated list of paths, defaults to \$PATH" | |
echo; | |
echo "Elements in PATHLIST which begin with a prefix from PREFIXLIST" | |
echo "are brought forward. Duplicates are removed." | |
echo "New colon separated list is sent to STDOUT" | |
echo; | |
echo "Example: " | |
echo " pathfix.sh '/sw:/usr' '/bin/:/usr/local/bin:/bin:/sw/bin'" | |
echo " returns: /sw/bin:/usr/local/bin:/bin" | |
echo; | |
exit 1; | |
fi | |
fi; | |
pathlist="$2"; | |
if [ -z "$pathlist" ]; then | |
pathlist="$PATH"; | |
fi | |
newpath=''; | |
oldpath=":$pathlist"; | |
OIFS="$IFS" | |
IFS=':' | |
for o in $prefixes; do | |
told=`echo "$oldpath"| cut -c 2-` | |
oldpath=''; | |
for p in $told; do | |
p=`echo "$p"|sed 's:/$::'`; | |
if echo "$p"|grep -q "^$o"; then | |
echo ":$newpath:" | grep -q ":$p:" || \ | |
newpath="$newpath:$p"; | |
else | |
echo ":$oldpath:" | grep -q ":$p:" || \ | |
oldpath="$oldpath:$p"; | |
fi | |
done | |
done | |
IFS="$OIFS" | |
newpath="$newpath$oldpath"; | |
echo "$newpath"| cut -c 2-; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be trivial to have the list of prefixes be specified as a parameter and to return the new path list. This would make the script reusable and not require "sourcing" it. For example
would become