2

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?

1
  • The problem has two parts: finding the second ServerName line, and inserting the six lines. Inserting the six lines is easy with sed's r command, but I'm having some trouble finding the second ServerName line... Commented May 18, 2017 at 0:06

2 Answers 2

1

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}"'
}'
Sign up to request clarification or add additional context in comments.

5 Comments

I think the question has the text to be inserted in the file named in $TEMPLATE. Fixing that doesn't have much impact on your script, but is closer to the question.
@JonathanLeffler: Thanks!
That looks promising, thanks. I am having trouble though getting it to complete the script because my While loop is reading each line of the Template file and replacing the ${SSLplaceholder} with domain.com on each line before inserting it into my config file. Placing the new sed statement inside the while loop causes the script to echo the contents of Template and not write to the config file, placing it outside the loop causes the script not to complete.
@DeltaMike: So dissociate the while loop from the config file. Have the while loop read the template file, make the replacement, and write it to another file, maybe call it sixlines. Then use the sed command to insert sixlines into the config file. (Not the only way, but perhaps the simplest.)
@Beta thanks so much - I got it figured out reverse engineering some other answers to similar questions
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.