How do you parse a variable in a bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do you parse a variable in a bash script?
# 1  
Old 03-18-2008
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  
Old 03-18-2008
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.
era
# 3  
Old 03-18-2008
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  
Old 03-18-2008
That does exactly what I needed. Again MANY thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Script to parse Perforce Logs

Hi All, I need to write a bash script that will parse some perforce log files, the log files will contain user login information, the script would need to pare the log, and check who logs in, and if the user is a superadmin, then the script will check the ip address to see which server the... (4 Replies)
Discussion started by: BostonRob
4 Replies

2. Shell Programming and Scripting

Bash script - cygwin (powershell?) pull from GitHub API Parse JSON

All, Have a weird issue where i need to generate a report from GitHub monthly detailing user accounts and the last time they logged in. I'm using a windows box to do this (work issued) and would like to know if anyone has any experience scripting for GitAPI using windows / cygwin / powershell?... (9 Replies)
Discussion started by: ChocoTaco
9 Replies

3. Shell Programming and Scripting

BASH script to parse XML and generate CSV

Hi All, Hope all you are doing good! Need your help. I have an XML file which needs to be converted CSV file. I am not an expert of awk/sed so your help is highly appreciated!! XML file looks like this: <l:event dateTime="2013-03-13 07:15:54.713" layerName="OSB" processName="ABC"... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

4. Shell Programming and Scripting

Bash Script for parse input like option and value

I would create a bash script than parse like this: test.sh -p (protocol) -i (address) -d (directory) I need retrive the value after -p for example... understand??? I hope... thanks (6 Replies)
Discussion started by: ionral
6 Replies

5. UNIX for Dummies Questions & Answers

bash script to parse sequence...

Hi, I have 4000 list files and 4000 sequence data files. Each list file contains a number of 'headers' and data file contains 'header and data'. I would like to extract data from the data file using the list file and write into a new file. As each of the files are quite large, an efficient piece... (6 Replies)
Discussion started by: Fahmida
6 Replies

6. Shell Programming and Scripting

Bash Shell Script to parse file

Raw Results: results|192.168.2|192.168.2.1|general/udp|10287|Security Note|For your information, here is the traceroute from 192.168.2.24 to 192.168.2.1 : \n192.168.2.24\n192.168.2.1\n\n results|192.168.2|192.168.2.1|ssh (22/tcp)|22964|Security Note|An SSH server is running on this port.\n... (2 Replies)
Discussion started by: jroberson
2 Replies

7. Shell Programming and Scripting

Parse config file data to script variable

I have a script with few pre defined variables. Also have a config file. Something like this. # config file # Define Oracle User MOD1_TAG=abcde MOD2_TAG=xyzabc MOD3_TAG=def I need to parse the config file and have each of the value copied to different variables. Please suggest what... (1 Reply)
Discussion started by: souryadipta
1 Replies

8. Shell Programming and Scripting

Parse file and copy value to variable in sh script.

Hi, I need to parse a simple text file like below and store the word that starts with BR* to a variable say $BRno. I need to do this in sh script. NOTE: the length of the numbers following BR is not constant (eg: it could be BR1234 or BR22233). And there is only 1 BRxxxxx in a file at a given... (6 Replies)
Discussion started by: script2010
6 Replies

9. Shell Programming and Scripting

Bash Script to read a file and parse each record

Hi Guys, I am new to unix scripting and I am tasked to parse through a CSV file delimited by #. Sample: sample.csv H#A#B#C D#A#B#C T#A#B#C H = Header D = Detail Record T = Tail What I need is to read the file and parse through it to get the columns. I have no idea on how... (8 Replies)
Discussion started by: 3vilwyatt
8 Replies

10. Shell Programming and Scripting

Need to Parse XML from bash script

I am completely new to bash scripting and now need to write a bash script that would parse a XML file and take out values from specific tags. I tried using xsltproc, xml_grep commands. But the issue is that the XML i am trying to parse is not UTF 8. so those commands are unable to parse my XML's... (4 Replies)
Discussion started by: shivashankar.g
4 Replies
Login or Register to Ask a Question