0

I am writing a script to automate few steps and need to create new configuration files with predefined location and text.

I am able to create the files however i am not sure how i can enter the text and save the files automatically using VIM.

Example:

Create a configuration file /etc/httpd/conf.d/cgi-enabled.conf with the following text

<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
</Directory>

and save it, this would allow me to move on to next configuration file. Any suggestions ?

1

If this is from a shell script I would recommend forgoing vim altogether and instead using echo, ie echo "sample text" > file.cfg this will create your file and fill it with the necessary content.

  • Aah! I was over-complexing things. Thank you! Will try this out. – Atul Apr 28 '17 at 22:38
3

No need to start vim, just use a here-document:

cat >/etc/httpd/conf.d/cgi-enabled.conf <<END_CONF
<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
</Directory>
END_CONF

The text between <<END_CONF and the last END_CONF delimiter will be saved to /etc/httpd/conf.d/cgi-enabled.conf through redirection by cat. The choice of delimiter is arbitrary, but one usually uses an uppercase word.

If you have shell variables (or things that looks like shell variables) in the here-document and you don't want these replace by their values, you should single-quote the first END_CONF:

cat >/etc/httpd/conf.d/cgi-enabled.conf <<'END_CONF'
<Directory "/var/www/html/cgi-enabled">
    Options +ExecCGI
    AddHandler cgi-script .cgi .pl
</Directory>

<!-- $HOME will not be expanded here -->
END_CONF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.