Randy McMurchy wrote:
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".
Is something staring me strait in the face that I'm not seeing? I
thought the expected output is the value of ${COUNTER}. I think I see
where you were going, but the example is incorrect...unless, like I
said, I'm just overlooking something really obvious.
My explanation (assuming that I haven't misunderstood the purpose of the
script): The very last '$' character is escaped so it is doing what it
was told to do: ie: replace 'regex that encompases the whole line' with
the the string "${COUNTER}" (not the value of ${COUNTER}).
-- DJ Lucas
--
http://linuxfromscratch.org/mailman/listinfo/blfs-dev
FAQ: http://www.linuxfromscratch.org/blfs/faq.html
Unsubscribe: See the above information page