The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #1 (permalink)  
Old 03-18-2008
vertical98 vertical98 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 8
How do you parse a variable in a bash script?

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes.

This is what I use on my apache server

#!/bin/bash
# accepts 3 parameters: <domain name> <user name> <XXXXXXXX>
# domain name is without www (just domain.com)
# username would be best at 6 - 10 chars long
# only checks if last is present and uses it for MySQL
# password. If not present - does not create mysql account
if [ "$3" != "" ]; then

--- snip ----

useradd $2 -m
filename=/etc/apache2/sites-available/$2.www
echo "<VirtualHost 10.10.10.10>" > $filename
echo "ServerAdmin webmaster@$1" >> $filename

--- snip some more -----

I now need to parse the first variable to add some characters:

So basically:

using example.com

#
# example.com
#
$HTTP["host"] =~ "(^|\.)example\.com$" {
server.document-root = "/home/example/public_html"
server.errorlog = "/var/log/lighttpd/example-error.log"
accesslog.filename = "/var/log/lighttpd/example-access.log"
server.error-handler-404 = "/e404-example.php"
}


Needs to become:

echo "#" >> $filename
echo "# %1" >> $filename
echo "#" >> $filename
echo "$HTTP[\"host\"] =~ \"(^|\.)<domain>\.<tld>$\" {" >> $filename
--- etc ----

I think the best way would be to parse until I hit the period and use the first part as one variable and the last part as another. I hope this makes sense.
Any assistance would be appreciated.

Alan