Unix Shell - question on bash construct: for i in `sth` loop

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2005 > question on bash construct: for i in `sth` loop





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 question on bash construct: for i in `sth` loop
joerg mauz

2005-04-28, 5:59 pm

hi all,

want i what to achive is to strip my PATH environment variable and get rid
of some directories..
there for i wrote a small script to do that for me. but the same script has
to run on a M$ plattform
using cygwin. now i'm in some trouble because of those blanks within M$
paths :-(

so here's an extract from my script:


for i in `echo PATH | awk '{ print }' RS=":"`; do
echo $i
done

let's say my PATH is "/cygdrive/d/program files/app1:/usr/bin:/sth/else:".
so now
my script produces:

/cygdrive/d/program
files/app1
/usr/bin
/sth/else

because of the blank in "/cygdrive/d/program files/app1" that is treated as
a delimiter for the "in" construct
in my for loop.

how can i rewrite my script to produce a $i for each loop action :

/cygdrive/d/program files/app1
/usr/bin
/sth/else

thanks in advance

cheers
joerg


Josh Howard

2005-04-28, 5:59 pm

joerg mauz wrote:

> for i in `echo PATH | awk '{ print }' RS=":"`; do
> echo $i
> done
>
> let's say my PATH is "/cygdrive/d/program files/app1:/usr/bin:/sth/else:".
> so now
> my script produces:


for i in `echo $PATH | awk '{ print }' RS=":"`;do
IFS=":" echo $i
done

or

echo $BLAH | awk -F":" '{for ( i = 1; i < NF; i++ ) {print $i}}'

would be two of a few ways of handling it.

--
Josh Howard <jrh@spam.pottedmeat.org>
William Park

2005-04-28, 5:59 pm

Josh Howard <jrh@spam.pottedmeat.org> wrote:
> joerg mauz wrote:
>
>
> for i in `echo $PATH | awk '{ print }' RS=":"`;do
> IFS=":" echo $i
> done


Have you tried it? :-)

--
William Park <opengeometry@yahoo.ca>, Toronto, Canada
Slackware Linux -- because it works.
Josh Howard

2005-04-28, 5:59 pm

William Park wrote:

> Have you tried it? :-)


I had IFS set to :, which seems to make it work in zsh, but I noticed
bash doesn't have the same behavior. 'oops'

--
Josh Howard <jrh@spam.pottedmeat.org>
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com