You gotta buncha stuff to look at here.
First, this is my cut-and-paste of what you said you did, with some echo statements to show what's going on:
Code:
#!/bin/sh
MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`
echo x001
echo $MYSUBSTRING
echo x002
echo $POSITION
echo x003
Did I copy it right?
Second, the output I got from running that was this:
Code:
x001
<factories xmi:type="resources.url:URL" xmi:id="URL
x002
x003
Note: no error message at all. Maybe I didn't copy what you did correctly. Please recheck.
Third, what's that $0 doing in there? In a bash command line, $0 is expanded to be the name of the running script if it's in a double quoted or back quoted string; it's left alone if it's in a single quoted string. I think that's what you want here, but you're not getting it. You're getting the name of the program, because this is really part of the back-quoted string; the single quotes within the back-quoted string don't count for squat when bash is interpreting the line; they just get sent through as they are, and bash doesn't even care if it finds an even number of them.
I discovered that by modifying this script somewhat (and saving it as e2, obviously):
Code:
#!/bin/sh
MYSUBSTRING="<factories xmi:type=\"resources.url:URL\" xmi:id=\"URL"
echo x000 "MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}"
POSITION=`awk -v MYSUBSTRING=$MYSUBSTRING '{ print index ($0, MYSUBSTRING)}' <<< ${line}`
echo x001
echo $MYSUBSTRING
echo x002
echo $POSITION
echo x003
... I got this output:
Code:
x000 MYSUBSTRING=<factories xmi:type="resources.url:URL" xmi:id="URL '{ print index (./e2, MYSUBSTRING)}' <<<
x001
<factories xmi:type="resources.url:URL" xmi:id="URL
x002
x003
See that ./e2 in there?
Anyway, either come back here with comments, or just break it down into simpler pieces so you can see what's going on.
Hope this helps.