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:

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:

#!/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

Assuming you need it inserted after the line "share declaration...." try as below instead of echo... statement

sed 's/share declaration .*/& \nveto oplocks = *.mdb/' /etc/samba/smb.conf > temp_file
mv temp_file to /etc/samba/smb.conf
1 Like

Wow fast response or what?? I assume my script will now look like this:

#!/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

Space not necessary between \n and veto. Also [] is not needed around share.
You may just run following:

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:

sed 's/shared.*/&\
veto oplocks = *.mdb/' /etc/samba/smb.conf
1 Like

thanks guys,

Will give it a go and see what happens :slight_smile:

---------- 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:

[global]
test stuff here

[shared]
more stuff

[other_share]
even more stuff

Here's the output of the running the command suggested by anurag:

:~# 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:

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 place:b:

Thanks chaps

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

So final script looks like:

#!/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

:slight_smile:
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?

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!

output from mv command:

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!! :confused::confused::confused:

Found one small problem with the line that I need to insert, it should be:

veto oplock files = /*.mdb/

not

veto oplock files = *.mdb

the problem is that if I add the two additional / to the script it no longer works, have also tried rapping the whole expression in " :wall:

Any help would be great

You will need to escape the forward slash in the sed's replacement part.

sed 's/share.*/&\nveto oplocks = \/*.mdb\//' /etc/samba/smb.conf > /etc/samba/temp_file
1 Like

As it turns out I need to add several lines to this file, I have tried repeating the command with a GOTO command (yes I know this is bad but it harps back to the old DOS days) but this clearly does not work for bash scripts or I can't get the syntax correct.

Anyway, would someone kindly help me modify the script to look for and then add 2 extra lines in the same place? The script currently looks like:

#!/bin/sh

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

I also need to add:

force user = smbadmin

and

oplocks = no

Thanks:)