Unix Shell - bash : how to loop through $PATH

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > bash : how to loop through $PATH





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author bash : how to loop through $PATH
clinton__bill@hotmail.com

2005-03-30, 7:55 am

Hi,
I want to loop through $PATH to get all the individual directories.
But the problem is for loop does not work when the deliminiter is
semicolon. I tried "sed -e "s/:/ /g" until I find that some directory
name has space in it.

Seems a common problem but I can not figure out a simple solution...

Janis Papanagnou

2005-03-30, 7:55 am

clinton__bill@hotmail.com wrote:
> Hi,
> I want to loop through $PATH to get all the individual directories.
> But the problem is for loop does not work when the deliminiter is
> semicolon. I tried "sed -e "s/:/ /g" until I find that some directory
> name has space in it.
>
> Seems a common problem but I can not figure out a simple solution...


for p in ${PATH//:/$'\n'} ; do echo ===$p=== ; done

IFS=:
for p in $PATH ; do echo ===$p=== ; done
# reset IFS if necessary


Janis
Chris F.A. Johnson

2005-03-30, 7:55 am

On Sat, 26 Mar 2005 at 00:56 GMT, clinton__bill@hotmail.com wrote:
> Hi,
> I want to loop through $PATH to get all the individual directories.
> But the problem is for loop does not work when the deliminiter is
> semicolon. I tried "sed -e "s/:/ /g" until I find that some directory
> name has space in it.
>
> Seems a common problem but I can not figure out a simple solution...


In bash or ksh93 (does not work if there are spaces in the dir
names; but there really shouldn't be in $PATH):

for dir in ${PATH//:/ }
do
: whatever
done


In any Bourne-type shell (works with spaces in names):

oldIFS=$IFS
IFS=:
set -- $PATH
IFS=$oldIFS
for dir
do
: whatever
done

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

2005-03-30, 7:55 am



clinton__bill@hotmail.com wrote:
> Hi,
> I want to loop through $PATH to get all the individual directories.
> But the problem is for loop does not work when the deliminiter is
> semicolon. I tried "sed -e "s/:/ /g" until I find that some directory
> name has space in it.
>
> Seems a common problem but I can not figure out a simple solution...
>


Try this:

path=`echo "$PATH" | sed -e 's/^/"/' -e 's/$/"/' -e 's/:/" "/g'`
for dir in $path
do
...
done

The first expression adds a leading double quote, the second adds
a trailing quote, the third changes all colons to a trailing quote
for the name in front and a leading quote for the name following.
It is left as an exercise to the reader to handle a PATH that
begins and/or ends with a colon.

Bill Seivert

Janis Papanagnou

2005-03-30, 7:55 am

Janis Papanagnou wrote:
> clinton__bill@hotmail.com wrote:
>
>
> for p in ${PATH//:/$'\n'} ; do echo ===$p=== ; done


And a variant to handle the spaces...

eval set - '${PATH//:/' '}'
for p ; do echo ===$p=== ; done

>
> IFS=:
> for p in $PATH ; do echo ===$p=== ; done
> # reset IFS if necessary


Janis
Chris F.A. Johnson

2005-03-30, 5:58 pm

On Mon, 28 Mar 2005 at 15:06 GMT, Stephane CHAZELAS wrote:
> 2005-03-26, 01:36(+00), Chris F.A. Johnson:
> [...]
>
> No, that ommits the empty components with the Bourne shell, and
> that ommits a trailing empty component with bash and some
> versions of ksh, and beware that IFS=$oldIFS doesn't restore the
> previous value of IFS if IFS was initially unset.


Good. The result should also be used to rebuild the PATH.

> Note that an empty path component (or an empty PATH) means the
> current directory should be searched.


The PATH variable should not contain empty components, as they are
interpreted as the current directory.

> tmp=`awk '
> BEGIN {
> n = split(ARGV[1] ":-", d, ":")
> for (i = 1; i < n; i++) {
> gsub(/'''/,"''\\\\\\\\'''", d[i])
> printf "%s", " '''" d[i] "'''"
> }
> }' "$PATH"`
> eval set -- "$tmp"
> for dir
> do
> : whatever
> done
>
> (you need a recent (1988) awk, which excludes Solaris /bin/awk)
>



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






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com