> Date: Thu, 20 Jun 2002 20:48:31 -0500
> From: "Jesse Angell" <[EMAIL PROTECTED]>
>
> I need to write a shell script to do the following
> edit the /www/conf/httpd.conf file
> and search for the
> <VirtualHost *>
> DocumentRoot /var/www/virtual/$user/html
> Servername $user.palaceunlimited.com
> ErrorLog /var/www/virtual/$user/logs/error_log
> CustomLog /var/www/virtual/$user/logs/access_log combined
> </VirtualHost>
> and then delete all of that for the specified user varible..
> So it looks for the user specified.. then deletes all of the users
> virtual host stuff including the start of the virtual host and the
> closing of it..
> Please help me out...
Not a shell script. You want awk or perl, though I suppose you
might want a shell script to run it. So....
#!/bin/sh
# ALWAYS put in the line above. It make sure that the script
# is read and interpreted by the correct shell.
if [ $# -ne 2 ]; then
echo "Usage: $0 username"
echo " Where username is the name of the user to be removed"
exit
fi
awk -v USERNAME=$1
"{ vh = ( $1 ~ /VirtualHost/)? 1 : 0; � if this is a start/end, set
switch
if ( $0 ~ USERNAME ) { # if this line has the user to go on
it,
thisone++; # make sure the switch is set
next; # and skip this line
}
if ( vh ) { # if this is a VirtualHost line,
if ( thisone ) # and it's the end of the user
thisone = 0; # reset the switch,
else # otherwise (it's the beginning)
save++; # set a switch that you just had a
start
next; # skip this line
} else # You can't get here, if the last
if ( save && ! thisone ) { # the last line was a start, and
this
save = 0; # isn't the user to go, unset the
switch
print "<VirtualHost *>"; # print the start
}
print $0; # print the line that you read in
}
END{
if ( ! thisone ) # if the last set was not the user to
go,
print $0; # make sure you print out the final stop
} /www/conf/httpd.conf > /tmp/httpd.tmp
if [ -z /tmp/httpd.tmp ]; then
if [ -e /www/conf/httpd.conf.old ]; then
rm /www/conf/httpd.conf.old
fi
mv /www/conf/httpd.conf /www/conf/httpd.conf.old
mv /tmp/httpd.tmp /www/conf/httpd.conf
fi
# end of script
$0 is the name of the script that's running, $# is the number
of arguments passed in - in this case, we *only* want the script
name, and the user to be terminated, $1-$n are the values of the
arguments.
In the awk command, -v tells it that you want to create a variable
that will be used inside the awk script, and lets you give it a
value. In this case, you've passed the username to it.
>From the original file, we create a tempfile, then, when done,
delete the old save file, if it exists, save the current file, and
write the new one...*if* the new one was correctly written (in
this case, has a file length > 0).
I have tested the awk script, though not the shell.
mark
--
"GUILTY, GUILTY, GUILTY!" - Megaphone Mark Slackmeyer, Doonesbury
_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list