使用 sed 修改 tomcat server.xml 配置

发布于 2024-12-12 09:47:28 字数 552 浏览 0 评论 0 原文

我正在制作一个通过 HTTPS 依赖于 Tomcat7 的 Ubuntu 软件包。为了方便我们的客户,我希望包的安装脚本在Tomcat7中启用HTTPS。这很容易手动完成;在文件 /etc/tomcat7/server.xml 中,需要取消注释以下块:

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

How can I do this from a shellscript?最好以某种方式,它仍然适用于对确切模式的轻微修改。我认为规则应该是搜索',然后删除

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆伤 2024-12-19 09:47:29

这可能有效:

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

这会忽略所有非注释行。将注释行保存在保留空间中,如果注释包含 ,则删除开始/结束注释分隔符,然后打印注释/非注释。

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.

你怎么这么可爱啊 2024-12-19 09:47:28

考虑在您的 server.xml 上应用补丁。

  1. 生成补丁文件:

    diff -ruN server.xml.old server.xml.new > mydiff.补丁
    

    其中 server.xml.old 是原始文件,server.xml.new 是您想要的文件。

    补丁(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 @@
     (...)
    
    - 
    
     (...)
    
  2. 然后,只需应用补丁:

     补丁 server.xml mydiff.patch
    

    您可以使用标志-N运行patch命令。因此,它将跳过看起来已经打过补丁的文件。

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.

贪了杯 2024-12-19 09:47:28

diff 最有可能是您选择的工具。但如果原始配置文件经常更改,diff 将无法在以后的版本中应用您的脚本。

sed 还具有读取多行的能力。您可能需要查看此示例它还涉及修改 xml 文档。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文