Hailwood
Hailwood

Reputation: 92601

Writing common file with replacements in bash

I am writing a script to setup new projects on my computer,

One part I am wanting to do is have it automatically setup the virtual hosts for me.

The file itself is quite simple

the file should be called <project>.testfox.dev.conf and should contain

<VirtualHost *:80>
DocumentRoot "/var/www/work/<project>"
ServerName <project>.testfox.dev
<Directory "/var/www/work/<project>">
allow from all
Options +Indexes
</Directory>
ErrorLog /var/log/apache2/<project>.error.log
LogLevel error
TransferLog /var/log/apache2/<project>.access.log
</VirtualHost>

What I wonder is, should I have that code in my script and write it out line by line to the file, or should I have a dummy file in the sites-available folder that I copy and then search a replace a placeholder?

Upvotes: 0

Views: 55

Answers (2)

Sorin
Sorin

Reputation: 5395

The most flexible way would be to have it as a template (either as a separate file, or a HEREDOC), writing it line-by-line would make it very hard to modify in future.

The HEREDOC approach would be easier to implement, as you can use variable interpolation:

cat >$PROJECT.testfox.dev.conf <<EOF

<VirtualHost *:80>
DocumentRoot "/var/www/work/$PROJECT"
ServerName $PROJECT.testfox.dev
<Directory "/var/www/work/$PROJECT">
allow from all
Options +Indexes
</Directory>
ErrorLog /var/log/apache2/$PROJECT.error.log
LogLevel error
TransferLog /var/log/apache2/$PROJECT.access.log
</VirtualHost>
EOF

Upvotes: 4

Olaf Dietsche
Olaf Dietsche

Reputation: 74048

Both are viable approaches.

Using it as a template makes it easier and more readable, if you want to modify it. But keeping it in a bash script as a here document works as well.

In the end, it depends on what your preferences are.

Upvotes: 1

Related Questions