Bruce Dubbs wrote these words on 02/28/06 19:12 CST:

> You have me interested.  Can you give an example?

Yes I can.

However, you must concede that if you agree there is difference in
behavior (one works, one does not), then it is worth switching. Okay,
the shell syntax is complicated, but I found a need to have to do it.

I will explain as I go.

First set a string variable

BIGSTRING="this is the string to test for"

Now I want to pick a word out of that string using sed. That word is
"string" and I must identify it by a variable.

MYSTRING="string"

Notice this is one word out of $BIGSTRING

We are in a loop right now with a variable being a counter of the
loop. Each repetition through the loop increments the counter.

COUNTER=1

Now I echo the string, sedding each line and if it is a match, then
I want to make a replacement based on the number of the counter.

So, the command is as follows (I won't insult you by explaining the sed):

echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/"

The word "string" was returned, as expected.

Now, we need to make the same expression so that it is assigned to
a variable. Here is where we get into trouble.

First using `...` syntax.

JUSTFORBRUCE=`echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/"`

We have assigned a variable:

echo $JUSTFORBRUCE

Woops, we didn't get what we wanted. We got ${COUNTER} instead

Now issue the *same exact command* using $(...) syntax:

JUSTFORBRUCE=$(echo ${BIGSTRING} | sed "s/^.*\($MYSTRING\).*$/\\${COUNTER}/")

Notice that now the desired string was returned: "string".

Need I say more?  :-)

-- 
Randy

rmlscsi: [GNU ld version 2.15.94.0.2 20041220] [gcc (GCC) 3.4.3]
[GNU C Library stable release version 2.3.4] [Linux 2.6.10 i686]
20:24:00 up 15 days, 4:33, 3 users, load average: 0.05, 0.50, 0.57
-- 
http://linuxfromscratch.org/mailman/listinfo/blfs-dev
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page

Reply via email to