1

I am trying to insert a string path stored in a variable into an xmlfile as the first part of an attribute value.

The xmlfile is a context.xml file for Tomcat using the form

<Context reloadable="true" docBase="application.war"> 
...
</Context>

A path, which is the absolute location of a war file to deploy is

$WKSP_APATH="/location/on/server

$WKSP PATH should be inserted as follows

<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>

I've tried to find information, but most sources focus on full line insertion. This is the last line to add for my script.

For context the executed line location is given below as an extract from the script it is embedded in

find /$SEARCH_PATH -name "*.xml" -type f|while read fname; do
    CONTEXT_FILE=${fname##*/}
    cp -rf "/$SEARCH_PATH/$CONTEXT_FILE" 
       "/$COPY_LOCATION/${PROJECT_CODENAME}-$CONTEXT_FILE"

    echo "Copied $CONTEXT_FILE to: 
        $COPY_LOCATION/$PROJECT_CODENAME-$CONTEXT_FILE for deployment"

    #do insert new path on local server here?

done

Many thanks

Maiakaat
  • 91
  • 8
  • 1
    Don't parse XML with regular expressions. http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Jordan Running Jul 05 '15 at 18:59
  • I don't want to get into a debate about parsing XML, all I need to do is insert a local location into a default context.xml file attribute. Looking at how that thread went I hope we can avoid it. This question and a solution is not easily found Online, but if there is a simple way to do this via an approved method then I hope the answer surfaces – Maiakaat Jul 05 '15 at 19:36
  • Is `application.war` a constant string or a variable one? – Eugeniu Rosca Jul 05 '15 at 19:45
  • it's a variable, the script basically provides a way to deploy a set of webapps from a git workspace directory, to one or more suitable servers, this feature is the last contribution. I guess an xml parser could be integrated if it could be used in a Bash script and it is the best/only viable solution, but I am a bit of a newbie with Linux. – Maiakaat Jul 05 '15 at 19:54

1 Answers1

2

Assuming that:

  • You take the risk to parse xml files using regex instead of proper tools.
  • The FILE variable contains the xml filename to process.
  • The WKSP_PATH variable contains the absolute location you want to insert as a prefix in the value of the docBase attribute.

Try:

#!/bin/bash
WKSP_PATH="/location/on/server"
sed -i "s@docBase=\"@docBase=\"$WKSP_PATH/@" "$FILE"

To check the result:

cat "$FILE"
<Context reloadable="true" docBase="/location/on/server/application.war">
...
</Context>
Eugeniu Rosca
  • 5,177
  • 16
  • 45
  • Thank-you again, this has worked perfectly. I'll be sure to look up a more reliable method for future versions. I will post the script Online with support docs too in the near future. – Maiakaat Jul 05 '15 at 20:19