DeltaMike
DeltaMike

Reputation: 33

While loop inserted with sed

I am trying to add multiple lines to a configuration file in the second occurrence of a string. I can get my While loop to append the code to the end of the file, but it needs to be inserted below a certain line and above another

<$site_conf tr '\n' '\0' |
sed -e "s/.*/$(yes '\0' | head -n 1 | tr -d '\n')/g" |
tr '\0' '\n' >> $site_conf
sed -i '0,/*:80/! {0,/*:80/ s/*:80/*:443/}' $site_conf
TEMPLATE='SSLEngine.txt'
while read LINE; do
    echo $LINE |
    sed 's/${SSLplaceholder}/'${SSLdomain}'/' >> $site_conf
done < $TEMPLATE;;

This produces a config file like so:

<VirtualHost *:80>
ServerName mysite.preview.something.com
DocumentRoot /home/mysite/web/content

<Directory /home/mysite/web/content>
Options +FollowSymLinks -Indexes
AllowOverride All
Require all granted
</Directory>

LogLevel warn
ErrorLog /home/mysite/web/log/mysite-error.log
CustomLog /home/mysite/web/log/mysite-access.log combined
</VirtualHost>
<VirtualHost *:443>
ServerName mysite.preview.something.com
DocumentRoot /home/mysite/web/content
<Directory /home/mysite/web/content>
Options +FollowSymLinks -Indexes
AllowOverride All
Require all granted
</Directory>

LogLevel warn
ErrorLog /home/mysite/web/log/mysite-error.log
CustomLog /home/mysite/web/log/mysite-access.log combined
</VirtualHost>
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5
SSLCertificateFile /etc/apache2/ssl/domain.com.crt
SSLCertificateKeyFile /etc/apache2/ssl/private/domain.com.key
SSLCertificateChainFile /etc/apache2/ssl/int.crt

I want the final 6 lines to be inserted under the second ServerName line and before the DocumentRoot line. Any ideas?

Upvotes: 2

Views: 207

Answers (2)

DeltaMike
DeltaMike

Reputation: 33

Ok - here is what finally worked fulfilling all of my requirements:

    ssltemplate="ssltemplate.txt"
    <$site_conf tr '\n' '\0' |
    sed -e "s/.*/$(yes '\0' | head -n 1 | tr -d '\n')/g" |
        tr '\0' '\n' >> $site_conf
    sed -i '0,/*:80/! {0,/*:80/ s/*:80/*:443/}' $site_conf
    TEMPLATE='SSLEngine.txt'
    while read LINE; do
        echo $LINE |
        sed 's/${SSLplaceholder}/'${SSLdomain}'/' >> $ssltemplate
    done < $TEMPLATE
    sed -i '1,/ServerName/{p;d}; /ServerName/,$ {1,1 r ssltemplate.txt
    }' $site_conf
    rm -r ssltemplate.txt

Upvotes: 0

Beta
Beta

Reputation: 99094

Here is a sed command that will do it, if the six lines are in the file whose name is in the variable TEMPLATE (and if I'm handling the variable correctly):

sed '/DocumentRoot/,/ServerName/ {
/ServerName/r '"${TEMPLATE}"'
}'

Upvotes: 1

Related Questions