LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-04-2005, 01:20 PM   #1
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Rep: Reputation: 30
$PATH checking


Hi!

I was wondering if someone has any thoughts about this short script for $PATH checking (delete the evil . and remove all duplicates + check if entries are really directories). Any suggestions are very welcome.

Code:
newpath=""

for i in `echo $PATH | sed -e "s/:/\n/g" | grep -v "\." | uniq`
do
  if [ -d $i ]
  then
    if [ "$newpath" == "" ]
    then
      newpath=$i
    else
      newpath=${newpath}:$i
    fi
  fi
done

export PATH=$newpath
unset newpath
any thoughts, optimisations ... ???

Enjoy
 
Old 01-04-2005, 01:42 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Code:
newpath=""

for i in $(echo $PATH | sed -e "s/:/\n/g" | grep -v "\." | uniq)
do
  if [ -d "$i" ]
  then
    newpath=$newpath:$i
  fi
done

export PATH=$(echo $newpath | cut -b 2-)
unset newpath
a little shorter. personally i'd not worry about the initialisation and unset, but that's just a bad habit on my part. I also put $i in quotes incase a space or a null string creeps in.

Last edited by acid_kewpie; 01-04-2005 at 02:25 PM.
 
Old 01-04-2005, 02:17 PM   #3
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Original Poster
Rep: Reputation: 30
Hi!

Code:
cut -b 2-
nice one, didn't even think about it. Just a question: any big difference between $() and `(backticks) ??

Enjoy
 
Old 01-04-2005, 02:25 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
to be honest i only recently found out about $() notation. AFAIK it's new-school as opposed to "retro" backticks. either work the same as far as i am aware, but it's nice to be consistent.
 
Old 01-04-2005, 02:35 PM   #5
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Original Poster
Rep: Reputation: 30
Yup, it seems thats it plus one more thing as seen in http://www.gnu.org/software/bash/man...ref.html#SEC30 :

Quote:
When the old-style backquote form of substitution is used, backslash retains its literal meaning except when followed by `$', ``', or `\'. The first backquote not preceded by a backslash terminates the command substitution. When using the $(command) form, all characters between the parentheses make up the command; none are treated specially.
Enjoy and thanx for the reply
 
Old 01-04-2005, 02:49 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
One other point to bring up... From the man page of uniq:
Quote:
Discard all but one of successive identical lines from INPUT (or standard
input), writing to OUTPUT (or standard output).
So, this:
Code:
for i in $(echo $PATH | sed -e "s/:/\n/g" | grep -v "\." | uniq)
ought to be changed to this:
Code:
for i in $(echo $PATH | sed -e "s/:/\n/g" | grep -v "\." | sort | uniq)
If not, identical paths separated by one or more other paths will not be removed. However, the sort will clearly screw up any specific ordering of your paths.
 
Old 01-04-2005, 03:03 PM   #7
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Original Poster
Rep: Reputation: 30
Code:
for i in $(echo $PATH | sed -e "s/:/\n/g" | grep -v "\." | sort | uniq)
the problem with this is that it sorts like this

/bin
/home
/usr/bin
...

which is not good (at least for mes) as sometimes you find files with similar names in /home that exist in /usr/bin and then there's a confusion. The problem with duplicates is actually a problem with my .bash_profile, where I have something like this:

Code:
export PATH=$PATH:$HOME/bin
and when I source it, it duplicates $HOME/bin.

Well there should be a solution waiting to be found. Untill then ...

Exactly what you said (Ireplied too fast)

Enjoy

Last edited by lfur; 01-04-2005 at 03:13 PM.
 
Old 01-04-2005, 05:10 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
There is another option. You can declare some functions in your ~/.bash_profile. I saw this technique in the LFS materials. In fact, that's exactly where the code comes from:
Code:
# Begin /etc/profile
# Written for Beyond Linux From Scratch
# by James Robertson <jameswrobertson@earthlink.net>
# modifications by Dagmar d'Surreal <rivyqntzne@pbzpnfg.arg>

#<snip>
   
# Functions to help us manage paths.  Second argument is the name of the
# path variable to be modified (default: PATH)
pathremove () {
        local IFS=':'
        local NEWPATH
        local DIR
        local PATHVARIABLE=${2:-PATH}
        for DIR in ${!PATHVARIABLE} ; do
                if [ "$DIR" != "$1" ] ; then
                  NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
                fi  
        done
        export $PATHVARIABLE="$NEWPATH"
}

pathprepend () {
        pathremove $1 $2
        local PATHVARIABLE=${2:-PATH}
        export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}

pathappend () {
        pathremove $1 $2
        local PATHVARIABLE=${2:-PATH}
        export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
}
Then, when you want to add a directory to your path, do something like:
Code:
pathappend $HOME/bin
The pathremove function will rip out any existing copy of a path you're trying to add, and since both pathprepend and pathappend always call pathremove, you're guaranteed to never get duplicates.
 
Old 01-05-2005, 10:25 AM   #9
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Original Poster
Rep: Reputation: 30
Removed uniq and added checking for duplicates:

Code:
newpath=""

for old in $(echo $PATH | sed -e "s/:/\n/g" | grep -v "\.")
do
  if [ -d "$old" ]
  then
    bad=0
    for new in $(echo $newpath | sed -e "s/:/\n/g")
    do
      if [ "$old" == "$new" ]
      then
        bad=1
      fi
    done
    if [ "$bad" == "0" ]
    then
      if [ "$newpath" == "" ]
      then
        newpath=$old
      else
        newpath=${newpath}:$old
      fi
    fi
  fi
done

echo $newpath
Any comments?

Enjoy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Image Path reference in Linux (Absolute path) javabuddy Linux - General 7 06-05-2006 07:45 AM
Apache path and kde path questions darkraider Debian 1 11-12-2005 05:07 AM
Set the path systemwide/Set the path for a user with Slackware jayhel Slackware 1 06-12-2005 12:24 AM
Removing path form PATH maginotjr Linux - Newbie 3 02-12-2005 02:23 PM
How to Chnage Python's module search path (sys.path)? lramos85 Linux - Software 1 05-02-2004 06:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration