Subject: Re: a shell idea?
To: VaX#n8 <vax@ccwf.cc.utexas.edu>
From: Simon J. Gerraty <sjg@zen.void.oz.au>
List: current-users
Date: 10/09/1995 21:30:20
> ``Add this directory iff it's not already in the path''
Very useful. My /etc/profile has had an add_path() function for
years. It does not check for the directory being in PATH because in
/etc/profile you _know_... the add_path() and friends used in
/etc/ksh.kshrc though do test for presence in PATH.
I recently extracted the functions into a separate file:
:
# NAME:
# add_path.sh - add dir to path
#
# DESCRIPTION:
# These functions originated in /etc/profile and ksh.kshrc, but
# are more useful in a separate file.
#
# SEE ALSO:
# /etc/profile
#
# AUTHOR:
# Simon J. Gerraty <sjg@zen.void.oz.au>
# RCSid:
# $Id: add_path.sh,v 1.1 1995/09/30 12:45:23 sjg Exp $
#
# @(#)Copyright (c) 1991 Simon J. Gerraty
#
# This file is provided in the hope that it will
# be of use. There is absolutely NO WARRANTY.
# Permission to copy, redistribute or otherwise
# use this file is hereby granted provided that
# the above copyright notice and this notice are
# left intact.
# is $1 missing from $2 (or PATH) ?
no_path() {
eval "case :\$${2-PATH}: in *:$1:*) return 1;; *) return 0;; esac"
}
# if $1 exists and is not in path, append it
add_path () {
[ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="\$${2:-PATH}:$1"
}
# if $1 exists and is not in path, prepend it
pre_path () {
[ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="$1:\$${2:-PATH}"
}
# if $1 is in path, remove it
del_path () {
no_path $* || eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: |
sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`
}
# we use this all the time too
Which() {
for d in `IFS=:; echo ${2-$PATH}`
do
test -x $d/$1 && { echo $d/$1; break; }
done
}
> ``Add this directory iff it [and every parent directory] are protected
> from writing by anyone but root or bin''
This is a good idea too, you could come up with a variation on the
above without too much effort.
--sjg