LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Add multiple lines to sites-available automatic (https://www.linuxquestions.org/questions/linux-server-73/add-multiple-lines-to-sites-available-automatic-612217/)

sinister1 01-09-2008 05:11 AM

Add multiple lines to sites-available automatic
 
Hai THere,

I am making a script that will automate the steps for adding a domain to a linux webserver.

How can i add multiple lines in a file with a variable in it.

The script works like this:

run script;
scripts asks which domain:
user enters domain;
The directories and symbolic links are made;
the user rights are set;
***NEED TO ADD BELOW LINES TO sites-available/$DOMAIN***
<VirtualHost ip address>
DocumentRoot /export/www/$DOMAIN/html
php_flag log_errors on
php_value error_log /export/www/$DOMAIN/logs/php_error.log
ServerName $DOMAIN
ErrorLog /export/www/$DOMAIN/logs/error.log
CustomLog /export/www/$DOMAIN/logs/access.log "combined"
HostNameLookups off
</VirtualHost>

How can i do this part? I can add them one at a time but can i put the whole text in a variable?

Thank you in advance.

Grt.

unSpawn 01-09-2008 05:46 AM

Two stupid examples.

A function with tab formatted VHost output:
addVH() { echo -en "\n<VirtualHost ip address>\n\tDocumentRoot /export/www/$1/html\n\tphp_flag log_errors on\n\tphp_value error_log /export/www/$1/logs/php_error.log\n\tServerName $1\n\tErrorLog /export/www/$1/logs/error.log\n\tCustomLog /export/www/$1/logs/access.log \"combined\"\n\tHostNameLookups off\n</VirtualHost>\n"; }
It takes domain name as single arg so you can do "addVH someDomain >> /some/config/file".

A "here" document:
Code:

DOMAIN="microsoft.com"
cat << EOL >> /some/config/file
<VirtualHost ip address>
 DocumentRoot /export/www/$DOMAIN/html
 php_flag log_errors on
 php_value error_log /export/www/$DOMAIN/logs/php_error.log
 ServerName $DOMAIN
 ErrorLog /export/www/$DOMAIN/logs/error.log
 CustomLog /export/www/$DOMAIN/logs/access.log "combined"
 HostNameLookups off
</VirtualHost>
EOL

Functions are good for repetitive things. Since they group code in blocks make code readable as well, use local variables and as bonus you get $FUNCNAME which comes in handy when reporting errors or progress (like 'echo "${0//*\//}: FATAL in ${FUNCNAME}()"').


All times are GMT -5. The time now is 07:11 AM.