简体   繁体   中英

Modify tomcat server.xml config using sed

I am making an Ubuntu package that depends on Tomcat7 through HTTPS. To make it convenient for our customers, I would like the install script of the package enable HTTPS in Tomcat7. This is pretty easy to do manually; in the file /etc/tomcat7/server.xml, one needs to uncomment the following block:

<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
           maxThreads="150" scheme="https" secure="true"
           clientAuth="false" sslProtocol="TLS" />
-->

How could I do this from a shellscript? Preferebly in a way that it still works for slight modifications of the exact pattern. I think the rule would be something along the lines of search for '<Connector port="8443"' and then remove <!-- and --> before and after the block.

Consider apply a patch on your server.xml .

  1. Generating a patch file:

     diff -ruN server.xml.old server.xml.new > mydiff.patch 

    Where server.xml.old is the original file, and server.xml.new is the file as you want.

    The patch ( mydiff.patch ) will look like this:

     --- server.xml.old 2011-10-29 04:03:25.000000000 -0300 +++ server.xml.new 2011-10-29 04:04:03.000000000 -0300 @@ -1,10 +1,10 @@ (...) - <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> - ---> (...) 
  2. Then, just apply the patch:

      patch server.xml mydiff.patch 

    You can run the patch command with the flag -N . Thus, it will skip files that seems already patched.

diff should most probably be the tool of your choice. But if the original config file is changed frequently, diff could not be able to apply your script in future versions.

sed also has the ability to read in more than one line. You may want to look at this example that also deals with modifying an xml document.

This might work:

 sed -nr '/^<!--/,/^-->/!{p;b};/^<!--/{h;d};H;/^-->/{x;/<Connector port="8443"/{s/(^<!--\s*\n|\n\s*-->)//g};p}'

This ignores all non-comment lines. Saves the comment lines in hold space then deletes the start/end comment delimiters if the comment contains <Connector port="8443" and then prints the comment/non-comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM