[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Edlug Archive Mar 2007 ]

Re: [edlug] Shell scripts




A nice way of feeding parameters to scripts:


while getopts  "d:t" flag
do
  case $flag in
  d) DATABASE=$OPTARG;;
  t) TABLE=$OPTARG;;
  *) echo "Usage: -d <database> [-t <table>]"
     exit -1;
  ;;
  esac
done

if [ -z "$DATABASE" ] ;
then
  echo "Usage: -d <database> [-t <table>]"
  exit -1
fi


So, this says, you MUST give a database and optionally a table. If you don't give a database it will display usage and exit.


I hope you find that useful!

Cheers,
David

Rodti MacLeary wrote:
From: Colin Shorts

Are you going to share it?

From: Andrew Smith


If you'd like any feedback on what you've written, send me a copy of your script.

I don't have shell access from work (boo! hiss!) so the nearest approximation to the one I wrote last night would be:


#!/bin/bash

touch /etc/apache2/vhosts/$1.conf
echo "<VirtualHost *>" > /etc/apache2/vhosts/$1.conf
echo "        ServerName www."$1"" >> /etc/apache2/vhosts/$1.conf
echo "        ServerAlias "$1"" >> /etc/apache2/vhosts/$1.conf
echo "        DocumentRoot /var/www/"$1"/htdocs/" >> /etc/apache2/vhosts/$1.conf
echo "</VirtualHost>" >> /etc/apache2/vhosts/$1.conf

mkdir /var/www/$1
mkdir /var/www/$1/cgi-bin
mkdir /var/www/$1/htdocs
mkdir /var/www/$1/logs
# End of script

I've called it newsite.sh and copied it to /bin. Running sudo newsite.sh domain.tld will create a domain.tld.conf file for Apache and the appropriate folder structure for the domain under /var/www. It's simple but I managed to transfer about ten sites from my old server in the time it would have previously taken me to do one. And of course it reduces the risk of typos!

It could probably do with sudo authentication within the script itself, possibly some validation to check if there actually is a parameter, and an Apache reload at the end to get the .conf file loaded. The authentication and validation are well documented in the FAQs posted yesterday but to be honest I can't be bothered!

The next step is to write a script to create mail users, although I've got to decide first what approach to take with email. A Postfix/Courier setup seems the best, as I want to run Mailman and Roundcube, but I'm still poring over different configuration suggestions and how-tos at the moment....

r.
-
----------------------------------------------------------------------
You can find the EdLUG mailing list FAQ list at:
http://www.edlug.org.uk/list_faq.html

-- David Hodgson Valley Technology Ltd. DDI: 0131-553-0401 - ---------------------------------------------------------------------- You can find the EdLUG mailing list FAQ list at: http://www.edlug.org.uk/list_faq.html



This archive is kept by wibble+RM@xxx.xxx.xxx
Morpheux
HomePage