Bash脚本更新ssl.conf中ssl证书文件的路径
我正在创建一个可重用的脚本,用于自动在服务器设置中设置新的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 (One has leading # tag):
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
我需要在bash脚本中对其进行动态设置,以使其最终像这样:
I need it to be dynamically set in the bash script, to end up like this:
SSLCertificateFile /etc/pki/tls/certs/example.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_CERT_PATH="SSLCertificateFile /etc/pki/tls/certs/localhost.crt"
SSL_CERT_PATH="SSLCertificateFile /etc/pki/tls/certs/example.crt"
sed "s/.*\b$SSL_DEFAULT_CERT_PATH\b.*/$SSL_CERT_PATH/" $SSL_CONFIG_PATH
***更新区域,还需要sed命令来更新以#开头的行.
***UPDATED AREA, Also need sed command to update lines that begin with #.
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"
我尝试了接受的
sed -i "s|.*\b#$SSL_DEFAULT_CA_CERT_PATH\b.*|$SSL_CA_CERT_PATH|" SSL_CONFIG_PATH
和
sed -i "s|(?s).*(?<!\\w)$SSL_DEFAULT_CA_CERT_PATH(?!\\w).*|$SSL_CA_CERT_PATH|" $SSL_CONFIG_PATH
都不起作用,因为正则表达式不正确.
neither are working because the regular expression is not correct.
问题在于斜线.您的变量包含它们,并且最终命令将具有多个正斜杠,这会影响您的
s/< search>/< replace/
语法.只需将您的 sed
单词分隔符从/
更改为 |
(或其他任何字符,例如说 @
或〜
)对其进行修复.
The problem is with slashes. Your variable contains them and the final command will have multiple forward slashes, which affect your original sed
syntax of s/
<search>/<replace/
syntax. Just change your sed
word separator from /
to |
(or any other character e.g say @
or ~
) to fix it.
sed -i "s|.*\b$SSL_DEFAULT_CERT_PATH\b.*|$SSL_CERT_PATH|" file
应该可以解决您的问题,并使用 -i
标志进行就地替换,并使用 -i.bak
以<文件名> .bak
should solve your problem and use the -i
flag to do in-place substitution and do -i.bak
to save a backup of the file in the format <filename>.bak
GNU sed
, man
页面说
/字符可以在任何给定的s命令中统一替换为任何其他单个字符./字符(或代替其使用的任何其他字符)仅在前跟\字符的情况下才可以出现在正则表达式或替换中.
The / characters may be uniformly replaced by any other single character within any given s command. The / character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a \ character.