Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Adding a new user to all groups where another user is

by bronto (Priest)
on May 12, 2008 at 09:48 UTC ( #686055=snippet: print w/ replies, xml ) Need Help??

Description: You just added a user to your UNIX system. You want him to belong to all groups where you belong. E.g.: let's say your user name is bronto and the new user is robin. Here we go.
perl -F: -i.save -ape 'if ($F[3] =~ m{bronto} and not m{robin}) { chom
+p ; $_ .= qq{,robin\n} }' /etc/group
Comment on Adding a new user to all groups where another user is
Download Code
Re: Adding a new user to all groups where another user is
by Limbic~Region (Chancellor) on May 12, 2008 at 17:59 UTC
    bronto,
    perl -F: -i.save -ape 'if ($F[3] =~ m{bronto} and not m{robin}) { chom +p ; $_ .= qq{,robin\n} }' /etc/group

    The first thing I see wrong with this is that you ignore the possibility that one user name may be contained within another. For instance - jon and jonathan.

    The second thing I see wrong is that you don't consider that a user's primary group need not be specified in /etc/group if it is present in /etc/passwd. For instance - bronto may belong to group 123 but not appear in group 123's line in /etc/group. At least on some *nix - which would mean 'robin' didn't end up in all groups as 'bronto'.

    The last thing is, your documentation is lacking. I know what -i.save does but would everyone who used it? What if someone made a typo - would they know how to undo it?

    Cheers - L~R

      Hi!

      Yep, every single byte you wrote is plain right. Thanks for taking the time to read and comment this node.

      I sometimes need to solve problems, like the one above, that require me a one-liner solution. When it happens, I often save the one liner in a file for future use, along with a very short comment to remind me what I wrote it for.

      I Often won't use it again as-is, but at least I have a good starting point for the next time.

      Basically, I did the same thing, publishing this one here instead of just saving it locally. So... have fun! :-)

      Ciao!
      --bronto


      In theory, there is no difference between theory and practice. In practice, there is.

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://686055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-07-26 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (176 votes), past polls