Shell script to modify file in several directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to modify file in several directories
# 1  
Old 09-28-2011
Shell script to modify file in several directories

Hi,

I want a script shell to automate modifying httpd.conf file for
several instances of apache, save httpd.file before changing it, after
modifying it and then restart apache.


- Replace ServerRoot "xxxx" by ServerRoot "yyyy" of all directories :
" /production/work/INSTANCE_NAME/apache/conf/httpd.conf " where
INSTANCE_NAME is the instance of apache (as you can see it, I have
many INSTANCE_NAME in /production/travail/)...

Thanks..
# 2  
Old 09-28-2011
Code:
# SAVE A BACKUP in case things go wrong!
tar -cf backup.tar /production/work/*/conf/httpd.conf

for INSTANCE in /production/work/*
do
        # Replace and save to a temp file, then overwrite the original.
        # Linux supports sed -i, which is more direct, but has the unfortunate
        # side effect of accidentally changing ownerships and permissions.
        # Overwriting it with 'cat' guarantees they won't change.
        sed 's/ServerRoot "yyyy"/ServerRoot "xxxx"/g' < $INSTANCE/apache/conf/httpd.conf > /tmp/$$
        cat /tmp/$$ > $INSTANCE/apache/conf/httpd.conf
done

rm -f /tmp/$$

# 3  
Old 09-29-2011
Many thanks !!

I have questions :

Is this script works for Solaris 10 ?
My second question is : how I can add commands which allow me to :
- delete a complete line like this : LoadModule status_module modules/mod_status.so
- comment (put #) for the line : Include /production/work/WEBLO/apache/conf/..... ?

Thanks

---------- Post updated 09-29-11 at 11:56 AM ---------- Previous update was 09-28-11 at 12:44 PM ----------

Hi

The script is not working. There are error in tar and sed :

tar: Removing leading `/' from member names
tar: /production/work/*/conf/httpd.conf: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors

sed: -e expression #1, char 64: unknown option to `s'


Also please tell me how I can :
- delete one or two line like this : LoadModule status_module modules/mod_status.so ;
- comment (put #) for the line : Include /production/work/WEBLO/apache/conf/.....

Thanks

---------- Post updated at 06:29 PM ---------- Previous update was at 11:56 AM ----------

Please help :
the sed command in this script delete all lines in httpd.conf file.
# 4  
Old 09-30-2011
Quote:
Originally Posted by bras39
tar: Removing leading `/' from member names
tar: /production/work/*/conf/httpd.conf: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
There was an error in the path. Should be /production/work/*/apache/conf/httpd.conf


Quote:
sed: -e expression #1, char 64: unknown option to `s'
I don't know what expression you fed into sed so I can't tell why it didn't work.
# 5  
Old 09-30-2011
Thanks Corona688,

The error is in xxx/yyy, sed don't like "/" !

And finaly I use this code which works fine :
Code:
sed -i.$(date '+%F')'s/ServerRoot "yyyy"/ServerRoot "xxxx"/' "$INSTANCE/apache/conf/httpd.conf"

After testing, I don't see any change in ownerships and permissions.

But can you please telling me how under the same conditions (i.e. for many directories) I can delete 2 lines like :
PPPP
LLLL

which follow each other or not in my httpd.conf file, and write in place of them another line like ZZZZ ?

Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

2. Shell Programming and Scripting

Modify XML tag using shell script

Hi All Need some help with a unix shell script. I have a XML file as shown below: <Root> <Service> <endPoint type="SOAP" protocol="http"> <provider>ABCD</provider> <urlRewrite>/service/xyz/getAccountDetails</urlRewrite> <timeout>30</timeout> </endPoint> </Service> <Service> <endPoint... (3 Replies)
Discussion started by: abhilwa
3 Replies

3. Shell Programming and Scripting

Modify cal command in shell script

Plz help me a) To display on the screen the sorted output of "who" along with the total no. of users. b) the same output (except the no. of users) should be in file FILE1. (2 Replies)
Discussion started by: shivasaini
2 Replies

4. Shell Programming and Scripting

Modify cal command in shell script

Plz help me To modify "cal " command to display calenders of the specified months. $ cal jan mar nov (0 Replies)
Discussion started by: shivasaini
0 Replies

5. Shell Programming and Scripting

Shell script to copy particular file from directories recursively

I have directory path in which there are several sub directories. In all these sub dir there will be one env.cnf file. I want to copy this env.cnf file from each sub dir's and place them in destination path by creating same filename as sub dir_env.cnf. After copying env.cnf files from source... (4 Replies)
Discussion started by: Optimus81
4 Replies

6. Shell Programming and Scripting

Modify text file using shell script

Hi, I have a text file which is following format - COL VAL ABC 1 ABC 2 ABC 3 ABC 4 ABC 5 My requirement is to search for a particular value (provided by user) in the file and comment the previous entries including that as well. E.g. If I search for number 3, then the output... (6 Replies)
Discussion started by: bhupinder08
6 Replies

7. Shell Programming and Scripting

Read data from .csv file through shell script & modify

I need to read data from a file called "test.csv" through shell script where the file contains values like name,price,descriptor etc. There are rows where descriptor (& in some rows name) are written as string & other characters like "car_+" OR "bike*" etc where it should contains strings like... (3 Replies)
Discussion started by: raj100
3 Replies

8. Shell Programming and Scripting

Help with a shell script to modify one line and copy the next 9 to same file

Hi everyone, the problem is quite simple, yet I can't find an easy solution using awk. I need to search for a string in $3, then if I find this string, copy the line,modify $3, and copy the next 9 lines to the same file. My problem is in the copying of the lines... Finding and modifying... (5 Replies)
Discussion started by: Teroc
5 Replies

9. Shell Programming and Scripting

how to modify a file using shell script

Hi, i am using SuonOS and ksh. i need to add data into a file(s.txt) using a shell script. i have to pass 3 parameters and these 3 paramaters should add into the file at end of the file. File s.txt is look like, --------------------------------- column1|column2|column3 ... (1 Reply)
Discussion started by: syamkp
1 Replies

10. Shell Programming and Scripting

Is it possible in a shell script to modify itself ??

We are running a quiz and the data collected from the quiz is submitted to the database. My requirement is to write a shell script to get these submitted records. I should be able to run this shell script at any time and the records it returns should be the ones submitted after the script was... (5 Replies)
Discussion started by: sehgalniraj
5 Replies
Login or Register to Ask a Question