Solaris bash script / search and append field to end of line in /etc/group

0

I need a bash script that will modify /etc/group to append and delete NIS users to specific local groups on a Solaris 10u8 system. Preferable one or two functions with uid and groupname as varibles.

inputfile before adding a user myuser to groupbbb in file /etc/group

...
groupaaa::98000:
groupbbb::98001:hisuser   
groupccc::98003:
...

outputfile

...
groupaaa::98000:
groupbbb::98001:hisuser,myuser
groupccc::98003:

... The function should check if user is aleady part of the local group and exit

Should I use sed or nawk or something else. Anyone have a nice oneliner :-)

In linux there is gpasswd but i havent found a corresponding command in Solaris. The user are not local on the system but NIS users so usermod will not work I think!

Greatful for any pointers!

/Smedis

user40797

Posted 2010-06-23T10:16:51.187

Reputation: 1

Answers

0

Call this function:

gradd () { local group=$1 user=$2; sed "/^${group}:/{/\<${user}\>/! s/$/,${user}/}' /etc/group; }

like this:

gradd groupbbb myuser > /tmp/newgroups && mv /tmp/newgroups /etc/group

The file redirection and renaming could be moved inside the function:

gradd () { local group=$1 user=$2; sed "/^${group}:/{/\<${user}\>/! s/$/,${user}/}' /etc/group  > /tmp/newgroups && mv /tmp/newgroups /etc/group; }

then the call would be:

gradd groupbbb myuser

Paused until further notice.

Posted 2010-06-23T10:16:51.187

Reputation: 86 075

Hi!

Many thanks, will check this out asap!

/Smedis – user40797 – 2010-06-23T10:45:08.513

sed -i will edit a file in place. – kmarsh – 2010-06-23T13:09:29.167

@kmarsh: It's not available on Solaris. – Paused until further notice. – 2010-06-23T14:07:11.570

OK, but if he is using Bash, chances are good that he has the rest of the Gnu tools installed. – kmarsh – 2010-06-23T16:50:51.367

I have Gnu tools installed :-) I'm had to switch to another projct but I'will report back! /Smedis – user40797 – 2010-06-24T13:00:19.663