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



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 12:25 PM
Parse String from a Variable racbern Shell Programming and Scripting 3 04-07-2008 12:04 PM
parse variable mpang_ Shell Programming and Scripting 2 01-03-2007 07:31 AM
bash env variable containing @ Yogesh Sawant Shell Programming and Scripting 4 12-20-2006 06:51 AM
parse a string variable methos Shell Programming and Scripting 3 10-18-2005 04:18 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #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
  #2 (permalink)  
Old 03-18-2008
era
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
So something like this?

Code:
#!/bin/bash

domain=${1%.*}
tld=${1#*.}

cat <<HERE
#
# $domain.$tld
#
\$HTTP["host"] =~ "(^|\.)$domain\.$tld\$" {
server.document-root = "/home/$domain/public_html"
server.errorlog = "/var/log/lighttpd/${domain}-error.log"
accesslog.filename = "/var/log/lighttpd/${domain}-access.log"
server.error-handler-404 = "/e404-$domain.php"
}
HERE
Example run:

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

I hope I got all the details right.

You can't really "parse" the variables much, you can perform simple string substitutions like I have but basically it's just $1 $2 $3 and if you need anything fancier, use some external utility.
  #3 (permalink)  
Old 03-18-2008
vertical98 vertical98 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 8
Thanks for the fast reply! The more I learn about BASH the more I realize I have to learn.

I'll try it out right now.
  #4 (permalink)  
Old 03-18-2008
vertical98 vertical98 is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 8
That does exactly what I needed. Again MANY thanks!
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0