Re: path keeps bloating with repeated entries on invoking subshells



Rahul <nospam@xxxxxxxxxxxxxx> writes:

In my tcshell I used to follow the construct:

set path=(./ ~/bin/ ~/bin/foocmds [snip]........ $path)

I thought this was the right way to go since I only append to the
systemwide path settings without overwriting anything as a user.

But yesterday I got a "word too long error" and on investigation I find
that each time I invoke a subshell (say, I made a change to the .cshrc or
added a new script to the bin) I find that the path keeps bloating up with
duplicate entries (I guess a side-effect of appending the $path at the
end).

Is there a smart-way around this?

Yes.

Loop thru the PATH looking for duplicates and remove the dupes.
Here's the logic in ksh:

#!/bin/ksh

# Modification History:

# Changed on 12/08/98 by Dan Espen:
# - New logic using "case" is faster, and fixes PATH,
# LD_LIBRARY_PATH, FPATH and MANPATH.

# Now fix the path by looking thru it for directories that don't exist,
# And duplicates:

pathfix(){
# Declare local variables
typeset pathin
typeset pathout
typeset anentry
typeset AllCriteria
typeset aCriteria
typeset flag
typeset holdme
typeset save_IFS
# save and set the IFS
save_IFS=$IFS
IFS=":${IFS}"
#
case $# in
1) AllCriteria="! -z -d" ; pathin=$1 ;;
2) AllCriteria=$1 ; pathin=$2 ;;
*) cat <<-! >&2
Usage: pathfix "tests" "path"
Example: PATH=$(pathfix "! -z -d" "$PATH")

will trim out all duplicate entries from the PATH, and will also
reject entries that are not directories '-d' and ones that are blank

Alt Usage: pathfix "path"
Example: PATH=$(pathfix "$PATH")

tests default to "! -z -d"

!
exit 2
;;
esac
#
# Loop through each entry in the pathin
#
for anentry in $pathin ; do
if [ "$anentry" != "" ]; then
flag=""
# Test agenst all of the AllCriteria
for aCriteria in $AllCriteria ; do
case $aCriteria in
!) holdme=$aCriteria ;;
*) if [ $holdme $aCriteria $anentry ]; then
:
else
flag="$holdme $aCriteria $anentry"
fi
holdme=""
;;
esac
done
# check for duplicate
case "${flag:+t}" in
"") case ":${pathout}:" in
*:${anentry}:*) ;;
*) pathout="${pathout}${pathout:+:}${anentry}" ;;
esac
;;
# t) ;; echo $0 $* --- $anentry --- flag=$flag
esac
fi
done
IFS="${save_IFS}"
echo $pathout
}

PATH=$(pathfix "! -z -d" "$PATH")
FPATH=$(pathfix "! -z -d" "$FPATH")
LD_LIBRARY_PATH=$(pathfix "! -z -d" "${LD_LIBRARY_PATH}")
MANPATH=$(pathfix "! -z -d" "$MANPATH")
export PATH FPATH MANPATH
.



Relevant Pages

  • Re: Debuggin ksh script function
    ... Hi,I have a question about debuggin in ksh. ... > typeset -ft addition ...
    (comp.unix.shell)
  • Re: colon
    ... ensures that the last command does not leave a non-zero exit code. ... The "typeset" builtin ... echo $USAGE ...
    (comp.unix.shell)
  • Re: ksh question
    ... Or use a loop on the values of the array, or use ksh93 ... associative arrays. ... typeset -A x ...
    (comp.unix.shell)
  • Re: Tomorrow´s Date
    ... # Extract Month, Day and Year from above output. ... typeset -Z2 Month ... # Print date in "mm/dd/yyyy" format. ...
    (comp.unix.shell)