ksh: add path to $PATH only when ot yet in $PATH

ksh: add path to $PATH only when ot yet in $PATH

Post by leo » Thu, 04 Mar 2004 10:46:59



hi there

how can i add a path (like "." or "/Users/Shared/bin") to the $PATH
environment var only when its not yet in there?

the nicest would be something where i can give the two new paths in one
string as input (eg. ".:/Users/Shared/bin") and $PATH as another input.

any idea?

thanks,leo

 
 
 

ksh: add path to $PATH only when ot yet in $PATH

Post by j.. » Thu, 04 Mar 2004 12:24:02



> how can i add a path (like "." or "/Users/Shared/bin") to the $PATH
> environment var only when its not yet in there?

> the nicest would be something where i can give the two new paths in one
> string as input (eg. ".:/Users/Shared/bin") and $PATH as another input.

One possibility (with no error checking):

#!/bin/sh
addToPath()
{
  case :$1: in
   *:$2:*)
         ;;
   *)    PATH=$PATH:$2; export PATH
         ;;
  esac

Quote:}

addToPath $1 $2
echo $PATH

Of course, this would have to be sourced rather than run as a command
to affect the calling process.

Joe
--
Everyone in this room is wearing a uniform, and don't kid yourself.
   - Frank Zappa

 
 
 

ksh: add path to $PATH only when ot yet in $PATH

Post by Chris F.A. Johnso » Thu, 04 Mar 2004 16:09:09



> hi there

> how can i add a path (like "." or "/Users/Shared/bin") to the $PATH
> environment var only when its not yet in there?

   It is generally considered bad practice to put . in one's PATH.

Quote:> the nicest would be something where i can give the two new paths in one
> string as input (eg. ".:/Users/Shared/bin") and $PATH as another input.

PATHLIST=".  /Users/Shared/bin  $HOME/bin"
for dir in $PATHLIST
do
  case :$PATH: in
      *:$dir:*) ;;
      *) [ -d $dir ] && PATH=${PATH:+$PATH:}$dir ;;
  esac
done

--
    Chris F.A. Johnson                  http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

1. add in the PATH a path if not present in the PATH

I look for a function in POSIX shell. this function must add path in the
$PATH if it isn't present.
by example:
I want add /bin in PATH
echo $PATH
--> /bin:/usr/bin
/bin is in the PATH then i don't add /bin
If I want add /usr/local/bin
/usr/local/bin isn't in the PATH then I add:
PATH=${PATH}:/usr/local/bin

  gaetan.schneller.vcf
< 1K Download

2. Strategies

3. zsh's 'typeset -U path' wipes out path/PATH

4. RH7.3, kernel 2.4.18-3 recompile to enable devfs

5. PATH=$(getconf PATH), but PATH for getconf?

6. Serial logoff not shown by "last"

7. set path = "$path" hoses path in tcsh -- why???

8. How to forward TELNET to other machine?

9. How to expand paths in $PATH like "~/bin" to full path?

10. how to add a path to my path variable

11. automatically adding paths to $PATH in startup files

12. Function to add path element to path variable

13. Add an absolute path prefix only to relative paths