Hi Richard,

Really quick scan, but it looks like you're loading a variable to run a
if on, basing it on grep (line 16 etc). You can do this directly, so
something like:

if grep -q ^$group: /etc/group; then
 echo "found one or more!"
else;
 echo "didn't find any :("
fi

The -q means grep is quiet, only returning via exit codes. Relevant man
page snippet:
-q, --quiet, --silent
Quiet;  do  not  write  anything  to  standard   output.    Exit
immediately with zero status if any match is found, even if an error was
detected.  Also see the -s  or  --no-messages  option. (-q is specified
by POSIX.)

HTH,

Johnathon

On 03/05/12 14:43, Richard Forth wrote:
> 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


-- 
All postal correspondence to:
The Positive Internet Company, 24 Ganton Street, London. W1F 7QY

The Positive Internet Company Limited is registered in England and Wales.
Registered company number: 3673639. VAT no: 726 7072 28.
Registered office: Northside House, Mount Pleasant, Barnet, Herts, EN4 9EE.

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

Reply via email to