Friday, January 28, 2011

How can I make this script better?

I was wondering if there is a better way to create a file other than appending each line to the file. I did it this way so I can preserve readability, however, there is no indentation. Is there a way to create a file and input multiple lines at once?

if [ -d "/srv/www/$1" ]; then
 echo "Domain name already exists!"
else 
 mkdir -p /srv/www/$1/public_html; 
 mkdir -p /srv/www/$1/logs; 
 echo "<VirtualHost>" > /etc/apache2/sites-available/$1
    echo "ServerAdmin support@$1" >> /etc/apache2/sites-available/$1
    echo "ServerName $1" >> /etc/apache2/sites-available/$1
    echo "ServerAlias www.$1" >> /etc/apache2/sites-available/$1
    echo "DocumentRoot /srv/www/$1/public_html/" >> /etc/apache2/sites-available/$1
    echo "ErrorLog /srv/www/$1/logs/error.log" >> /etc/apache2/sites-available/$1
    echo "CustomLog /srv/www/$1/logs/access.log combined" >> /etc/apache2/sites-available/$1
 echo "</VirtualHost>" >> /etc/apache2/sites-available/$1
 a2ensite $1
  • Use a heredoc.

    cat > /etc/apache2/sites-available/"$1" << EOF
    <VirtualHost>
    ServerAdmin support@$1
    ...
    EOF
    
    Doug : 8 minutes until you get best answer
  • Heredoc as stated in answer #1 or just have your echo go across multiple lines

    echo "line 1
    line2
    line3" > file
    
    From Arun

0 comments:

Post a Comment