script for inserting line at specific place in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script for inserting line at specific place in file
# 1  
Old 12-29-2010
[SOLVED] script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the appropriate share declaration in the smb.conf file:
Code:
veto oplocks = *.mdb

I know that zentyal will over write any changes I make directly to the file and will have to use a script in the /etc/ebox/hooks/ directory. My problem is adding the line to the correct part of the file. Here's my script:
Code:
#!/bin/sh

if grep "veto oplocks" /etc/network/interfaces >/dev/null; then
        exit 0
fi
echo veto oplocks = *.mdb >> /etc/samba/smb.conf

exit 0

The problem is that I need it inserted after the share declaration [share], this script currently places it at the end of the file.

Any suggestions? Thanks

Last edited by barrydocks; 12-29-2010 at 02:48 PM..
# 2  
Old 12-29-2010
Assuming you need it inserted after the line "share declaration...." try as below instead of echo... statement
Code:
sed 's/share declaration .*/& \nveto oplocks = *.mdb/' /etc/samba/smb.conf > temp_file
mv temp_file to /etc/samba/smb.conf

This User Gave Thanks to michaelrozar17 For This Post:
# 3  
Old 12-29-2010
Wow fast response or what?? I assume my script will now look like this:

Code:
#!/bin/sh

if grep "veto oplocks" /etc/samba/smb.conf >/dev/null; then
        exit 0
fi
sed 's/[shared] .*/& \n veto oplocks = *.mdb/' /etc/samba/smb.conf > /etc/samba/temp_file
mv /etc/samba/temp_file to /etc/samba/smb.conf

exit 0

Is there a space between /n and veto? and do I need to include the [] around the share declaration as this is how it is written in the smb.conf file?

Thanks again
# 4  
Old 12-29-2010
Space not necessary between \n and veto. Also [] is not needed around share.
You may just run following:
Code:
sed 's/shared.*/&\nveto oplocks = *.mdb/' /etc/samba/smb.conf

And see the output on screen is as expected or not.
If above doesn't work:
Code:
sed 's/shared.*/&\
veto oplocks = *.mdb/' /etc/samba/smb.conf

This User Gave Thanks to anurag.singh For This Post:
# 5  
Old 12-29-2010
thanks guys,

Will give it a go and see what happens Smilie

---------- Post updated at 06:17 PM ---------- Previous update was at 11:41 AM ----------

OK tried this with limited success. This is the content of my test /etc/samba/smb.conf file:
Code:
[global]
test stuff here

[shared]
more stuff

[other_share]
even more stuff

Here's the output of the running the command suggested by anurag:
Code:
:~# sed 's/shared.*/&\nveto oplocks = *.mdb/' /etc/samba/smb.conf
[global]
test stuff here

[shared]
veto oplocks = *.mdb
more stuff 

[other_share]
even more stuff

Which is great but the smb.conf file remains unchanged. Then I tried the relavant bit from michaels post:
Code:
sed 's/share.*/& \nveto oplocks = *.mdb/' /etc/samba/smb.conf > /etc/samba/temp_file

and bingo, the temp_file contains the content of the smb.conf file plus the additional line in the correct placeSmilie

Thanks chaps

---------- Post updated at 06:23 PM ---------- Previous update was at 06:17 PM ----------

So final script looks like:
Code:
#!/bin/sh

if grep "veto oplocks" /etc/samba/smb.conf >/dev/null; then
        exit 0
fi
sed 's/share.*/& \nveto oplocks = *.mdb/' /etc/samba/smb.conf > /etc/samba/temp_file
cp /etc/samba/temp_file  /etc/samba/smb.conf

exit 0

Smilie
Interestingly, the mv command produced an error so I used cp instead but it also deletes the temp_file, didn't think this should happen?

Last edited by barrydocks; 12-29-2010 at 02:45 PM..
# 6  
Old 12-30-2010
Post the error you got while using mv command. Before that check for permissions in the smb.conf file that could be the reason as well. cp command should not delete the original file (temp_file, here) hmm..its weird!
# 7  
Old 12-30-2010
output from mv command:
Code:
mv: target `/etc/samba/smb.conf' is not a directory

I ran this as root so there shouldn't be any permissions problems?

Having said all that, it's now working fine!! SmilieSmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to place specific contents filename within text file

I am trying to use awk to place the contens of a filename in $1 and $2 followed by the data in the text file. Basically, put the filename within the text file. There are over 1000 files in the directory and as of now each file is saved with a unique name but it is not within the file. Thank you... (10 Replies)
Discussion started by: cmccabe
10 Replies

2. Shell Programming and Scripting

How to merge variable data from another file into specific place?

Hello, I'm trying to create multiple commands using a variable input from another file but am not getting any successful results. Basically, file1.txt contains multiple lines with single words: <file1.txt> yellow blue black white I want to create multiple echo commands with these... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

Commenting a specific line and inserting a new line after commented line.

Hello All, I have following file contents cat file #line=aaaaaa #line=bbbbbb #line=cccccc #line=dddddd line=eeeeee #comment=11111 #comment=22222 #comment=33333 #comment=44444 comment=55555 Testing script Good Luck! I would like to comment line line=eeeeee and insert a new line... (19 Replies)
Discussion started by: manishdivs
19 Replies

4. Shell Programming and Scripting

Inserting file after specific line in another file

Im attempting to insert the contents of File1 at a specific point of File2. File1 AD004 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 File2 AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AB002 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AC003 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 Result AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

5. Shell Programming and Scripting

Inserting text into a file but searching for the place to put it!

Hi I would love a bit of help with a problem im having with a script. I need to insert a line of text which is saved in a variable called $fwInsert into a file whos name is saved in a variable called $server but it needs to be in a certain order. The file is a forward file for a network and... (12 Replies)
Discussion started by: KatieV
12 Replies

6. Shell Programming and Scripting

Read from file specific place in file using inode

Hello, I am using tcsh on AIX. I would like to write a script that does the following: 1. given an inode, how do I find exactly the name of the file? I know I could do this using ls -i | grep <inode> but it returns: <inode> <filename>. I need some string manipulation or something to... (1 Reply)
Discussion started by: lastZenMaster
1 Replies

7. Shell Programming and Scripting

Problem inserting text into file after specific line

this is utterly embarassing :( after posting here i revisited my files and found that when i used "vi" instead of a gui based editor, i suddenly found that the indentations were in fact wrong :( sorry about this :( (0 Replies)
Discussion started by: mocca
0 Replies

8. Shell Programming and Scripting

Jump to a specific place in a file?

If I cat a file And want to go to the first instance of a particular value - what command would I use? And then from that point where I jumped to search for another value - but only search from that point forward not before the file? Thanks~ (2 Replies)
Discussion started by: llsmr777
2 Replies

9. Shell Programming and Scripting

Splitting av file in 2 at specific place based on textpattern

I have a file that I want to split in 2 (with Bourne shell sh) preferably. The file consists of groups of lines separated by newline. The file can vary in length, so I need to check number of groups of text. Here's an example ====EXAMPLE START==== #fruit banana #color yellow #surface smooth... (0 Replies)
Discussion started by: borgeh
0 Replies

10. Shell Programming and Scripting

insert file 1 at a specific place of file 2

Hello, I need to search in file2 for class A : public B { and insert right after that the content of file1. I am a bit lost as to which tools (which bash functions, awk...). I should use. Thanks for some directions here. Regards (1 Reply)
Discussion started by: JCR
1 Replies
Login or Register to Ask a Question