Bash脚本更新ssl.conf中SSL证书文件的路径(RegEx单词与#匹配) [英] Bash script to update path to SSL certificate file in ssl.conf (RegEx word match with #)

查看:139
本文介绍了Bash脚本更新ssl.conf中SSL证书文件的路径(RegEx单词与#匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个可重用的脚本,用于自动在服务器设置上设置新SSL.我有几行需要更新文件路径.

I am creating a reusable script for automating the setup of new SSLs on server setups. I have a few different lines that need to get the file paths updated.

ssl.conf文件中的默认值看起来像这样(带有#号标记):

The defaults in the ssl.conf file look like this (Has leading # tag):

#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt

我需要在bash脚本中对其进行动态设置,以使其最终像这样:

I need it to be dynamically set in the bash script, to end up like this:

 SSLCACertificateFile /etc/pki/tls/certs/example-ca-bundle.crt

到目前为止,我是从这个开始的,但是我不确定自己在做什么.

So far I started out with this, but i'm not sure what I'm doing.

~/update_ssl_conf.sh

代码:

#!/bin/bash
SSL_CONFIG_PATH="/etc/httpd/conf.d/ssl.conf"

SSL_DEFAULT_CA_CERT_PATH="#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt" 
SSL_CA_CERT_PATH="SSLCACertificateFile /etc/pki/tls/certs/example-ca-bundle.crt"

我尝试从下面链接的可接受的解决方案开始,然后在\ b之后添加#.

I tried starting with the accepted solution linked below, and adding in a # after \b.

sed -i "s|(?s).*(?<!\\w)$SSL_DEFAULT_CA_CERT_PATH(?!\\w).*|$SSL_CA_‌​CERT_PATH|" $SSL_CONFIG_PATH

我觉得两者都不起作用,因为正则表达式不是100%正确的.

I have a feeling neither are working because the regular expression is not 100% correct.

推荐答案

您可以使用带有()的组来匹配表达式组:

You can use groups with () to match group of expressions :

SSL_CONFIG_PATH=/etc/httpd/conf.d/ssl.conf

SSL_CA_CERT_PATH=/etc/pki/tls/certs/example-ca-bundle.crt

sed -ri "s|(.*#)(SSLCACertificateFile)\s+(.*)|\2 $SSL_CA_CERT_PATH|" $SSL_CONFIG_PATH

  • (.*#)匹配评论
  • (SSLCACertificateFile)匹配字段名称
  • (.*)匹配值
    • (.*#) match the comment
    • (SSLCACertificateFile) match the field name
    • (.*) match the value
    • 在新路径后面仅需输入字段名称(第2组\2)

      Only the field name (group 2 \2) is required following by your new path

      这篇关于Bash脚本更新ssl.conf中SSL证书文件的路径(RegEx单词与#匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆