Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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.
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.
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 ...
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.