#!/bin/sh # This is designed to be sh, ksh, bash compatible # ------------------------------------------------------------------------------ buildpath () { ( dbg=false if [ $# -gt 0 ]; then if [ x"$1" = x-d ]; then dbg=true shift fi fi export dbg if [ $# -eq 0 ]; then cat 1>&2 <<-EnD `basename $0` [-d] directory-or-search-path [directory-or-search-path] ... -d Produces debug output on the standard error stream Search paths are split at colons into directory path elements; these and any plain directory path arguments are read in order, checked to make sure the directory exists, and added, at most once, with colon separators, to the output. The result is a new colon-separated search-path, written to the standard output stream. EnD else ( for dp in "$@"; do case "$dp" in *:*) # split args containing colons into lines on stdout echo "$dp" | tr : '\n' ;; *) # just output non-colon-containing args one per line echo "$dp" ;; esac done ) | ( IFS="" # do not allow space separators inside lines i=0 while read d; do # read a line into $d case ":${p}:" in *:${d}:*) # $d has already been seen: ignore duplicate $dbg && echo "dupl $d" 1>&2 ;; *) if [ -d "$d" ]; then # $d is a directory: add it to path $p i=`expr $i + 1` # or --> i=$(($i + 1)) $dbg && echo `printf "%4d %s" $i $d` 1>&2 p="$p$sep$d" sep=":" else # $d is not a directory $dbg && echo "!dir $d" 1>&2 fi ;; esac done # all done: echo the new path $p echo $p ) fi ) } buildpath "$@"