shell script - identify duplicate paths

What is the easiest way to perform a check from a shell script (preferably
csh) to identify if a path is already included. Is there a simple command
to:

   Determine if a path is already included in
   a shell environment variable
      (e.g., $PATH, $LD_LIBRARY_PATH, $MANPATH, etc.)

   Determine if a fontpath has already been identified
   to the X server (e.g. xset +fp)


I would like to be able to do something like the following:

   set YY_APP_PATH  = /USERS/logins/yy_app/bin
   set YY_APP_FONTS = /USERS/logins/yy_app/fonts

   if !( $YY_APP_PATH in $PATH ) then
      setenv PATH ${PATH}:$YY_APP_PATH
   endif

   if !( $YY_APP_FONTS in xset_Font_Path ) then
      xset +fp $YY_APP_FONTS
   endif
lhgarrettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dgrimesCommented:
Although not very elegant, this will work. Don't feel bad about rejecting it!

#!/bin/csh
#
# Validate the presence/absence of a path value within a path variable.
#
# Two variables are passed to a function that parses the path variabe to check for the presence of a path string.
#
# var1=The path variable to be parsed
# var2=The path string to be checked
#

chkpth() {
pth="$1"

while true
do
        p1=`echo $pth | cut -f1 -d':'`
        pth=`echo $pth | cut -f2- -d':'`
        [ "$p1" = "$2" ] && return 0
        [ "$p1" = "$pth" ] && return 1
done
}

if chkpth "/abc:/def/zzz:ghi:/jkl" "ghi"
then
        echo yes
else
        echo no
fi
0
lhgarrettAuthor Commented:
> Although not very elegant, this will work...

I'm hoping to find a command(s) provided by the operating system if possible, although I agree this is one approach for parsing a string. If there isn't an operating system command, is this the most efficient way of doing it?

By the way, isn't this a #!/bin/sh  script instead of #!/bin/csh ?   :-)
0
ozoCommented:
#!/bin/csh
foreach p ($path)
  if( "$p" == "$YY_APP_PATH" ) then
    unsetenv YY_APP_PATH
    break
  endif
end
if( $?YY_APP_PATH ) then
  setenv PATH ${PATH}:$YY_APP_PATH
endif

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ahoffmannCommented:
echo $path | tr ' ' '\012'|uniq -d
echo $MANPATH|tr ':' '\012'|uniq -d
0
ozoCommented:
Doesn't that require $path to be sorted?
0
ahoffmannCommented:
opps, needs to be piped through sort after tr
THANKS ozo
0
lhgarrettAuthor Commented:
So I've come up with two versions of a script (that are combinations of dgrimes, ozo and ahoffman comments!!!). Each script handles three kinds of field separators (colon blank comma), for example,

   $PATH           one:two:three
   $path           one two three
   Font Path       one,two,three

chkpth1

   #!/bin/csh -f
   foreach p ( `echo $2 | tr ',' ' ' | tr ':' ' '` )
      if ( $p == $1 ) exit 0
   end
   exit 1

chkpth2

   #!/bin/csh -f
   echo $1 $2 | \
        tr ' ' '\012' | \
        tr ',' '\012' | \
        tr ':' '\012' | \
        sort | uniq -d | \
        grep ^${1}$ >&! /dev/null
   exit $status

To use the script:

   set new_path = /usr/dt/bin
   if ! ( { chkpth1 $new_path $PATH } ) setenv PATH ${PATH}:$new_path
   unset new_path
0
lhgarrettAuthor Commented:
Please help me with who should get points, or with more comments. I think everyone contributed something to the approach.

Also, can I put chkpth1 into a #!/bin/csh script as a function, as dgrimes did in the first proposed solution?
0
dgrimesCommented:
>By the way, isn't this a #!/bin/sh  script instead of #!/bin/csh ?


I tested with csh and it worked just fine for me. csh supports much of the same functionality sh does. :)
0
dgrimesCommented:
I didn't mean to submit as an answer.
0
ahoffmannCommented:
> Also, can I put chkpth1 into a #!/bin/csh script as a function, as dgrimes did in the first proposed solution?
NO, csh has no functions, but goto ;-)

> setenv PATH ${PATH}:$new_path
you can put this in a script file too, but keep in mind that each subshell has it's own environment, which is destroyed at exit (or EOF).
I assume that you want to export the computed PATH to the parent, then you have
to source the script, like

source new_script
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lhgarrettAuthor Commented:
> source new_script

You are correct, I am sourcing a new_script that has a foreach statement in it to call the chkpth1 script for each new path I anticipate adding to an existing path.
0
lhgarrettAuthor Commented:
> Please help me with who should get points

   25 points for ozo for the 'foreach and sort' part of solutions (see Points for ozo)

   50 points to ahoffman for the 'tr and uniq' portions of the solutions; and subsequnt answer to the 'NO, csh has no functions' answer

   I hope this is OK... Thanks to everybody!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.