47896bf5fa21a1a8ee1c6a4af0ba2448e868c2bc
[platform/upstream/bash.git] / examples / functions / pathfuncs
1 #From: "Simon J. Gerraty" <sjg@zen.void.oz.au>
2 #Message-Id: <199510091130.VAA01188@zen.void.oz.au>
3 #Subject: Re: a shell idea?
4 #Date: Mon, 09 Oct 1995 21:30:20 +1000
5
6
7 # NAME:
8 #       add_path.sh - add dir to path
9 #
10 # DESCRIPTION:
11 #       These functions originated in /etc/profile and ksh.kshrc, but
12 #       are more useful in a separate file.
13 #
14 # SEE ALSO:
15 #       /etc/profile
16 #
17 # AUTHOR:
18 #       Simon J. Gerraty <sjg@zen.void.oz.au>
19
20 # RCSid:
21 #       $Id: add_path.sh,v 1.1 1995/09/30 12:45:23 sjg Exp $
22 #
23 #       @(#)Copyright (c) 1991 Simon J. Gerraty
24 #
25 #       This file is provided in the hope that it will
26 #       be of use.  There is absolutely NO WARRANTY.
27 #       Permission to copy, redistribute or otherwise
28 #       use this file is hereby granted provided that
29 #       the above copyright notice and this notice are
30 #       left intact.
31
32 # is $1 missing from $2 (or PATH) ?
33 no_path() {
34         eval "case :\$${2-PATH}: in *:$1:*) return 1;; *) return 0;; esac"
35 }
36 # if $1 exists and is not in path, append it
37 add_path () {
38   [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="\$${2:-PATH}:$1"
39 }
40 # if $1 exists and is not in path, prepend it
41 pre_path () {
42   [ -d ${1:-.} ] && no_path $* && eval ${2:-PATH}="$1:\$${2:-PATH}"
43 }
44 # if $1 is in path, remove it
45 del_path () {
46   no_path $* || eval ${2:-PATH}=`eval echo :'$'${2:-PATH}: |
47     sed -e "s;:$1:;:;g" -e "s;^:;;" -e "s;:\$;;"`
48 }