From: Kush on
Hi. I'm not sure of some 'egrep' functionality.

I have: string1=PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/
bin:/bin:/usr/bin
(basically PATH statements from /etc/rc.d/init.d directory).

The following is my code so far and what I am trying to do is to find
either the current working directory at the beginning of the path
string or in the middle and also checking to see if the PATH statement
starts with a "$". In the above case it actually starts with a double
quote and then the $ sign. #!/bin/bash

search_path=/etc/rc.d/init.d
for i in `ls -al $search_path | awk ' { print $9}'`; do
# echo "checking file " $search_path/$i
string1=`grep "PATH=" $search_path/$i`
if [ ! -z "$string1" ]
then
needed=`echo $string1 | awk ' {print substr($1, 6)}'`
# Check for current working directory in the beginning or middle of
the PATH statement
echo $string1 | egrep "(^.:|:.:$) | ("PATH=$")"
if [ $? == 0 ]
then echo "Current working directory found in " $string1
fi
fi


Question: Is my egrep statement not correct? Is there a simpler way of
achiving this?


From: Barry Margolin on
In article
<cd49220a-e117-46cb-aef7-ed5d9d022377(a)r27g2000yqb.googlegroups.com>,
Kush <kushvinder.rai(a)scotiabank.com> wrote:

> Hi. I'm not sure of some 'egrep' functionality.
>
> I have: string1=PATH="${IPSEC_SBINDIR}":/sbin:/usr/sbin:/usr/local/
> bin:/bin:/usr/bin
> (basically PATH statements from /etc/rc.d/init.d directory).
>
> The following is my code so far and what I am trying to do is to find
> either the current working directory at the beginning of the path
> string or in the middle and also checking to see if the PATH statement
> starts with a "$". In the above case it actually starts with a double
> quote and then the $ sign. #!/bin/bash
>
> search_path=/etc/rc.d/init.d
> for i in `ls -al $search_path | awk ' { print $9}'`; do

Why are you using ls -l instead of just ls? Also, why are you using -a,
since that will make it include the "." and ".." directories? There
shouldn't be any scripts starting with "." in that directory. I'd
change it to:

for i in $search_path/*; do

> # echo "checking file " $search_path/$i
> string1=`grep "PATH=" $search_path/$i`
> if [ ! -z "$string1" ]
> then
> needed=`echo $string1 | awk ' {print substr($1, 6)}'`

Bash can do substring extraction itself:

needed=${string1:5}

Or you can avoid hard-coding the number 5 and use:

needed=${string1#PATH=}

> # Check for current working directory in the beginning or middle of
> the PATH statement
> echo $string1 | egrep "(^.:|:.:$) | ("PATH=$")"

I think you meant to echo $needed. You also need to use single quotes,
because $ is treated specially inside double quotes.

echo $needed | egrep '^.:|:.:|^"?$'

> if [ $? == 0 ]
> then echo "Current working directory found in " $string1
> fi
> fi
>
>
> Question: Is my egrep statement not correct? Is there a simpler way of
> achiving this?

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Barry Margolin on
In article
<0003fefb-ae1a-4055-8113-297896e49948(a)d37g2000yqm.googlegroups.com>,
Kush <kushvinder.rai(a)scotiabank.com> wrote:

> I just did a 'Reply'. I'll take it out for next time.

You definitely need to learn how to edit your replies. Did you really
need to quote the entire thread just to answer that one tangential
question? And t was a rhetorical question, you weren't even expected to
answer it in the first place.

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***