Skip to content

Instantly share code, notes, and snippets.

@erluko
Created December 8, 2012 22:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save erluko/4242255 to your computer and use it in GitHub Desktop.
Save erluko/4242255 to your computer and use it in GitHub Desktop.
Fix the mess that /usr/libexec/path_helper made of my $PATH
#!/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-;
@erluko
Copy link
Author

erluko commented Dec 8, 2012

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

 . ./fixpaths.sh

would become

 PATH=`fixpaths.sh '/Users/:/sw/:/usr/local'`

@erluko
Copy link
Author

erluko commented Dec 8, 2012

So trivial that I fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment