Hi Peeps,

I wanted to share with you a working BASH script that basically takes a
textfile that looks kinda like this:

sales:mark,sally,dave,sue,steve
marketing:jason,philip,eric,steve
...

and processes each line - lets take the first line as example:

Checks to see if sales exists already in /etc/group
If not creates it.
then checks to see if each user exists, if they do
it checks to see if they are already in the group,
if not it adds it to the group
if the user doesnt exist it creates the user and assigns a random 8
character password, then adds it to the group.
If you want one particular user to be in more than one group thats ok just
add it to whatever line for that group (see steve in the example).

Its only 49 lines of code, including documentation notes.

Probably totally impractical in real life but it was an exercise in bash
coding, with this aim in mind.

If anyone wants a copy, I've created a public pastebin:
http://pastebin.com/uPxsZcGr

the textfile was originally called bugalist


     8          for line in `cat bugalist`


You can probably modify line 8 to be $1 or set up some code to check for
arguments etc, which I now realise I missed out, and didnt document either
(damn).

Anyway just in case you wonder what "bugalist" was:

~/Dev_Area/bash $ cat bugalist
u5:john,tessa,richard,steve
u6:dorris,erran,jason,steve
~/Dev_Area/bash $

Sample output:
~/Dev_Area/bash $ sudo ./buga
===========
Processing Line:
u5:john,tessa,richard,steve
===========
        Creating group 'u5' ...
                Members: john,tessa,richard,steve
                        Creating user 'john' ...
                        Password for user 'john' set to: vM6oElDG
                        Adding user 'john' to group 'u5' ...
                        uid=1002(john) gid=1003(john)
groups=1003(john),1002(u5)

                        Creating user 'tessa' ...
                        Password for user 'tessa' set to: t9jt2uRR
                        Adding user 'tessa' to group 'u5' ...
                        uid=1003(tessa) gid=1004(tessa)
groups=1004(tessa),1002(u5)

                        Creating user 'richard' ...
                        Password for user 'richard' set to: vZddQN4g
                        Adding user 'richard' to group 'u5' ...
                        uid=1004(richard) gid=1005(richard)
groups=1005(richard),1002(u5)

                        Creating user 'steve' ...
                        Password for user 'steve' set to: RG8fdZjG
                        Adding user 'steve' to group 'u5' ...
                        uid=1005(steve) gid=1006(steve)
groups=1006(steve),1002(u5)

===========
Processing Line:
u6:dorris,erran,jason,steve
===========
        Creating group 'u6' ...
                Members: dorris,erran,jason,steve
                        Creating user 'dorris' ...
                        Password for user 'dorris' set to: qDTgCcxn
                        Adding user 'dorris' to group 'u6' ...
                        uid=1006(dorris) gid=1008(dorris)
groups=1008(dorris),1007(u6)

                        Creating user 'erran' ...
                        Password for user 'erran' set to: JOL47SrN
                        Adding user 'erran' to group 'u6' ...
                        uid=1007(erran) gid=1009(erran)
groups=1009(erran),1007(u6)

                        Creating user 'jason' ...
                        Password for user 'jason' set to: 3UG416G6
                        Adding user 'jason' to group 'u6' ...
                        uid=1008(jason) gid=1010(jason)
groups=1010(jason),1007(u6)

                        User 'steve' already exists.
                        Adding user 'steve' to group 'u6' ...
                        uid=1005(steve) gid=1006(steve)
groups=1006(steve),1002(u5),1007(u6)


Enjoy!

Feedback welcome.

Enjoy!
Rich
_______________________________________________
Peterboro mailing list
Peterboro@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/peterboro

Reply via email to