我正在制作一个通过 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.
发布评论
评论(3)
这可能有效:
这会忽略所有非注释行。将注释行保存在保留空间中,如果注释包含,则删除开始/结束注释分隔符,然后打印注释/非注释。
This might work:
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.考虑在您的
server.xml
上应用补丁。生成补丁文件:
其中
server.xml.old
是原始文件,server.xml.new
是您想要的文件。补丁(
mydiff.patch
)将如下所示:然后,只需应用补丁:
您可以使用标志
-N
运行patch
命令。因此,它将跳过看起来已经打过补丁的文件。Consider apply a patch on your
server.xml
.Generating a patch file:
Where
server.xml.old
is the original file, andserver.xml.new
is the file as you want.The patch (
mydiff.patch
) will look like this:Then, just apply the patch:
You can run the
patch
command with the flag-N
. Thus, it will skip files that seems already patched.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.