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.
The defaults in the ssl.conf file look like this (One has leading # tag):
SSLCertificateFile /etc/pki/tls/certs/localhost.crt
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
Code:
#!/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
***UPDATED AREA, Also need sed command to update lines that begin with #.
The defaults in the ssl.conf file look like this (Has leading # tag):
#SSLCACertificateFile /etc/pki/tls/certs/ca-bundle.crt
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
Code:
#!/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"
I tried from the accepted solution for the
sed -i "s|.*\b#$SSL_DEFAULT_CA_CERT_PATH\b.*|$SSL_CA_CERT_PATH|" SSL_CONFIG_PATH
and
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.
The problem is with slashes. Your variable contains them and the final command will have multiple forward slashes, which affect your original sed
syntax ofs/
<search>/<replace/
syntax. Just change your sed
word separator from /
to |
(or any other character eg say @
or ~
) to fix it.
sed -i "s|.*\b$SSL_DEFAULT_CERT_PATH\b.*|$SSL_CERT_PATH|" file
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
The GNU sed
, man
page says
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.