views:

1079

answers:

4

Hi,

I am a newbie to shell scripting and to Linux environment as well.

In my project I am trying to search for following text from the httpd.conf file

<Directory '/somedir/someinnerdir'>
AllowOverride All
</Directory>

and then remove this text and again rewrite the same text. The reason to do this rewriting is that the script will be run on first installation of the web app, but it may again be run some time later as other part of this shell script is performing other tasks as well. So for first time this text wont be found and will simply be written but later again when script is run this text will be found and will need to be removed and the written again.

So the part of my script with which I am trying to achieve this is something like :

grep -ve "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" /etc/httpd/conf/httpd.conf > tmp_direct

echo -e "<Directory '/somedir/someinnerdir'>\\nAllowOverride All\\n</Directory>" >> tmp_direct

mv tmp_direct /etc/httpd/conf/httpd.conf

I dont have the code in front of me currently so there may be some syntactical errors above but the logic/coding is same.

Above code fragment is not able to do what I want to achieve as the grep command doesnt support multiline searching.

My OS is Fedora 8.

Can you please suggest something in this code to achieve what is needed or may be some other alternative.

Any help in this regard will be highly appreciated.

Thanks in advance.

A: 

If you want to make sure you're matching an entire line, you could do: grep -v "^AllowOverride All$"

The ^ matches the start of line and $ matches the end of line.

Does that help? I'm not exactly clear on what you're trying to do with the grep.

Steve
+1  A: 

This sounds like a job for sed(1). What you want will be very close to

sed -i.bak -e '/AllowOverride All/i# '  file

to comment out the line (remember httpd.conf has comments) followed by

sed -i.bak -e 's/# AllowOverride All/AllowOverride All '  file

to pujt it back.

WARNING I haven't tried it, you want to read the man page and test it youself.

Charlie Martin
A: 

I'm unsure I understand the question. Correct me if I'm wrong but your trying to add 'AllowOverride All' to the httpd.conf on the 1st running of your script. If so do you want it enabled?

Then on a 2nd pass you may want to enable / disable the command?

If I remember correctly 'AllowOverride' is only allowed in a < Directory > section So it only makes sense to add / enable in < Directory >< /Directory > definition:

If above is true you could do this in perl..

1st run: to add

perl -0777 -pi -e 's{(.?(?!AllowOverride\s+All).?)}[\nAllowOverride All\n$1]ixgs' httpd.conf

This will add 'AllowOverride All' to a Directory section only if it does not already contain one.

2nd run: to alter

On the 2nd run you could comment out as Charlie's suggested.. or if you like perl

perl -0777 -pi -e's{(.?)(AllowOverride\s+All)(.?)}[$1#$2$3]ixgs' httpd.conf

and to comment in

perl -0777 -pi -e's{(.?)(#AllowOverride\s+All)(.?)}[$1AllowOverride\s+All$3]ixgs' httpd.conf

The regex s{}[] is quite verbose and there are better regex's to do the same thing but this is easier ( hopefully ) for a beginner to understand :).

Explanation of the perl command options

-0777 force Perl to read the whole file in one shot because 0777 is not a legal character value

-p places a loop around your script

-i lets you edit files in-place ( so your command in '-e' change the file )

-e lets you specify a single line of code on the command line

In that single line we have a regex. see perldoc perlre for info on that.

Hope that helps

i-moan
A: 

Thanks for your replies.

Sorry for the previous bad code. Its corrected now.

Charlie and i-moan, due to workability constraints I wont be able to implement sed or perl as it will need to be added to the environment we will distribute this project in.

Steve, I want to do the check for multiple lines. I didnt put it in code blocks so it removed the directory tags. :(

So I will need to find some other way out.

Thanks again.

Best regards.