【问题标题】:Cut Apache virtual host block切 Apache 虚拟主机块
【发布时间】:2022-01-16 13:13:45
【问题描述】:

我正在尝试从使用 bash 的文件中删除某些虚拟主机。

例如。在我的脚本中,我想获得使用the_one_known2.example.com 的虚拟主机,但另一次是the_one_known3.example.com,即我想获得一些预先知道的URL(存在于ServerName 或ServerAlias 中)的部分形式的apache 配置文件,该URL 设置在bash 脚本作为参数。

<VirtualHost *:80 *:${SERVER_PORT}>
    ServerName      test1.example.com
    ServerAlias     test2.example.com
    ServerAlias     the_one_known1.example.com
    ServerAlias     test3.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
    ServerName      test4.example.com
    ServerAlias     the_one_known2.example.com
    ServerAlias     test5.example.com
    ServerAlias     test6.example.com
</VirtualHost>
<VirtualHost *:80 *:${SERVER_PORT}>
    ServerName      the_one_known3.example.com
    ServerAlias     test7.example.com
    ServerAlias     test8.example.com
    ServerAlias     test9.example.com
</VirtualHost>

所以我的 URL 变量将更改为例如。 the_one_known2.example.com我会得到:

<VirtualHost *:80 *:${SERVER_PORT}>
    ServerName      test4.example.com
    ServerAlias     the_one_known2.example.com
    ServerAlias     test5.example.com
    ServerAlias     test6.example.com
</VirtualHost>

[编辑] 到目前为止,我试图找到所选虚拟主机的第一行:

url_line=$(grep -n -m 1 "${URL}" ./apache.conf" | sed  's/\([0-9]*\).*/\1/')
vitual_host_start_line="$((app_line-1))" // this is an assumption that Virtual Host starts just one line before the first occurance of URL
echo $vitual_host_line // the place it starts

但是我很难找到这个虚拟主机的最后一行,因为它是&lt;/VirtualHost&gt;vitual_host_start_line 之后的第一次出现

【问题讨论】:

    标签: bash apache shell awk


    【解决方案1】:

    使用您展示的示例,请尝试遵循awk 代码。用 GNU awk 编写和测试。

    awk -v RS='<VirtualHost[^/]*/' -v ORS="VirtualHost>\n" 'RT~/[[:space:]]+the_one_known2\.example\.com\n/{print RT}' Input_file
    

    解释: 简单的解释是,使 RS(记录分隔符)从 &lt;VirtualHost 到下一个 / 出现(非贪婪匹配)。然后检查它是否有[[:space:]]+the_one_known2\.example\.com\n,如果是则打印匹配值,这将在段落结束时打印VirtualHost&gt;,因为它在程序中设置为ORS值。

    【讨论】:

      【解决方案2】:

      awk:

      awk -v name="the_one_known2.example.com" 'BEGIN{RS=ORS="</VirtualHost>\n"} $0~name{print; exit}' file
      

      我假设the_one_known2.example.com 不是您文件中另一个域的子字符串。

      见:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-19
        • 2019-08-25
        • 2012-04-26
        • 2012-09-25
        • 2011-03-25
        • 2018-06-26
        • 1970-01-01
        • 2011-05-04
        相关资源
        最近更新 更多