简体   繁体   English

使用sed修改tomcat server.xml配置

[英]Modify tomcat server.xml config using sed

I am making an Ubuntu package that depends on Tomcat7 through HTTPS. 我正在制作一个依赖于Tomcat7通过HTTPS的Ubuntu软件包。 To make it convenient for our customers, I would like the install script of the package enable HTTPS in Tomcat7. 为了方便我们的客户,我希望软件包的安装脚本在Tomcat7中启用HTTPS。 This is pretty easy to do manually; 这很容易手动完成; in the file /etc/tomcat7/server.xml, one needs to uncomment the following block: 在文件/etc/tomcat7/server.xml中,需要取消注释以下块:

<!--
<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? 我怎么能从一个shellcript做到这一点? 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. 我认为规则将是search for '<Connector port="8443"' ,然后在块之前和之后删除<!---->

Consider apply a patch on your server.xml . 考虑在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. server.xml.old是原始文件, server.xml.new是您想要的文件。

    The patch ( mydiff.patch ) will look like this: 补丁( mydiff.patch )将如下所示:

     --- 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 . 您可以使用标志-N运行patch命令。 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. 但是,如果经常更改原始配置文件,则diff可能无法在将来的版本中应用您的脚本。

sed also has the ability to read in more than one line. sed还具有读取多行的能力。 You may want to look at this example that also deals with modifying an xml document. 您可能希望查看此示例该示例还涉及修改xml文档。

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. 将注释行保存在保留空间中,如果注释包含<Connector port="8443" ,则删除开始/结束注释分隔符,然后打印注释/非注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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