2

I'm very new to SH and development and I'm blundering my way through the code one error at a time.

I've come across this error now that I can not get to the bottom of. Any help would be great!

I am running a sh script in GitHub Actions and am receiving this error:

sed: -e expression #1, char 73: unterminated `s' command

Here's my code:

SPECTESTSFILE='manifest/specifictests.xml'
  cat $SPECTESTSFILE

BUILDXML_TEMPLATE_FILE='buildFiles/buildrunspecifictests.template.xml' SPECTESTS="$(cat $SPECTESTSFILE)" echo "Specified Tests: " echo $SPECTESTS sed -i "s|<runTest></runTest>|$SPECTESTS|g" $BUILDXML_TEMPLATE_FILE

In my manifest/specifictests.xml file, I have a list of RunTests with new lines

<runTest>...</runTest>
<runTest>....</runTest>

and I want to insert this list into the buildFiles/buildrunspecifictests.template.xml file

<deploy>
    <runTest></runTest>
</deploy>

Eventually, I want the file to look like this:

<deploy>
   <runTest>...</runTest>
   <runTest>....</runTest>
</deploy>

What I've found is that if there are no newlines in the manifest/specifictests.xml file then it works but this doesn't allow me much freedom for the user's using it or eventually automating the creation of this file elsewhere.

Does anyone know a fix?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287

1 Answers1

2

It seems that you've slurped the entire contents of the file 'manifest/specifictests.xml' into the variable SPECTESTS, and you're instructing sed to search through the file 'buildFiles/buildrunspecifictests.template.xml' and replace every occurence of the string "<runTest></runTest>" with the entire contents of SPECTESTS.

As you've noticed, this is highly liable to break if there are any special characters in specifictests.xml. In particular, the newlines in $SPECTESTS are interrupting the middle of the "s" command, as the error says; the newlines would have to be escaped for sed to keep reading the command.

It sounds like all you actually want to do is prepend "<deploy>" and append "</deploy>" to the contents of specifictests.xml? Is that right?

In which case, you could just do

{ echo "<deploy>"; cat "$SPECTESTSFILE"; echo "</deploy>"; } > "$BUILDXML_TEMPLATE_FILE"

If that won't work, then to specifically work around newlines in $SPECTESTS, you can replace them with something else before feeding it to sed, and then turn them back into newlines after sed is done, like this:

SPECTESTS="$(tr '\n' '\r' < $SPECTESTSFILE)"
sed "s|<runTest><\/runTest>|$SPECTESTS|g" $BUILDXML_TEMPLATE_FILE | tr '\r' '\n'

This puts the result on standard output, not back into buildrunspecifictests.template.xml. (But it seems like you might want the result in a different file anyway, otherwise you'll break future substitutions.)

This is still susceptible to other problematic characters showing up in $SPECTESTS, though, like |, &, or \.

You can try the solutions at sed command to replace a string from one file with entire contents of another file.

Nick Matteo
  • 1,485