LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   File Copying to Same File (https://www.linuxquestions.org/questions/linux-newbie-8/file-copying-to-same-file-745352/)

Plato 08-05-2009 01:19 PM

File Copying to Same File
 
Having a problem trying to make a command that will copy part of a file to another part of a file. smb.conf file is the file that is being edited. Appending an already creating valid users list, to another share with the same valid users. The reason for this is so whenever new users are added with another script, they are appended to Share1, this will be updating the new share with newly created users on Share1. Will i need to use awk or sed?

i.e.
[Share1]
valid users = user1, user2, user3, user4, user5, user6, user 7

[Share2]
valid users = (add all Share1 valid users here)

Many thanks in advance

synss 08-05-2009 04:08 PM

I do not understand what you are trying to do.

If you want to append to a file, use redirection >>.
If you want to write to different files with a single command, use tee(1).
If you want to append something at the end of a specific line in a file, use sed(1), see, e.g., http://www.linuxquestions.org/questi...e-done-684650/

Plato 08-05-2009 04:19 PM

Sorry for not making much sense in my post.

Basically what I want is to have Share1's Valid Users be appended to Share2's when the share is created. Currently, I have a script that will attach new users to Share1. When I make a new share, with another script, I want the all the users for Share1, to be added to Share2.

p.s. Share2 isn't created yet, but when it is, I'd like to have all Share1's users be added to it.

DotHQ 08-05-2009 04:26 PM

couldn't you simply copy Share1 to Share2 right after creating share2? If there is a chance there is already a member in share2 I would use the append such as:
cat share1 >> share2
This will add share1 to the end of share2

Plato 08-05-2009 04:46 PM

DotHQ, let me explain the situation a bit further..

The problem is that there won't be a user to maintain the system / smb.conf file, so I'm writing scripts that will make new shares and add existing users. There will be someone who can log into terminal and run scripts, but nothing more than that.

I have a script that adds users to samba, and then add them to a root share (directory), which then has many other files inside of it, which are all shares themselves for speecific users and have specific permissions for each.

Since there will be new users being added, i'd like to copy the valid users from Share1 (since it will have every user there is on the system), to the new Share2. A simple (not for me lol), cp from Share1's Valid users to be mimiced to Share2's Valid users.

I'm not even sure if I just made sense.. :(

Kenhelm 08-05-2009 07:55 PM

This uses GNU sed.
It replaces every subsequent 'valid users =' line with the first one.
Code:

sed '/^[[:blank:]]*valid users =/{
h
:a n; /^[[:blank:]]*valid users =/g; ba
}' infile > outfile

# Input
[Share1]
valid users = user1, user2, user3, user4, user5, user6, user 7

[Share2]
valid users = <Anything here will be overwritten>

[Share3]
valid users = <Anything here will be overwritten>

# Output
[Share1]
valid users = user1, user2, user3, user4, user5, user6, user 7

[Share2]
valid users = user1, user2, user3, user4, user5, user6, user 7

[Share3]
valid users = user1, user2, user3, user4, user5, user6, user 7


Plato 08-06-2009 10:04 AM

Kenhelm,

Unfortunately the code you suggested ended up deleting my entire smb.conf file. :doh:

Code:

sed '/^[[:blank:]]*valid users =/{
h
:a n; /^[[:blank:]]*valid users =/g; ba
}' infile > outfile


Kenhelm 08-06-2009 01:14 PM

If you mean the output file was empty:
Which version of sed are you using?
Could you post your smb.conf file in code tags to preserve spacing?
I've just tried the sed code on my own smb.conf file and it works as expected.

If you mean the input file was deleted:
Perhaps you used the same file for both input and output.
'infile' and 'outfile' should be different or the contents of the file will be deleted due to redirection '>'.
sed doesn't delete the input file unless the '-i' option is used.

Plato 08-06-2009 04:00 PM

Well, your right, I did do /etc/samba/smb.conf > /etc/samba/smb.conf, assuming it would edit itself. I figured a solution out though, which won't require me to even make a share, but to just give new folders that are added 777 permissions so all users can wrx them.. less clutter this way I suppose.

Thanks again for you help everyone.

synss 08-07-2009 12:48 AM

Quote:

Originally Posted by Plato (Post 3634042)
but to just give new folders that are added 777 permissions so all users can wrx them.. less clutter this way I suppose.

That looks dangerous, what about having the users part of a common group and setting the permission to 775?


All times are GMT -5. The time now is 09:11 PM.