LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Script to add a secondary group to all users except system default users (https://www.linuxquestions.org/questions/linux-server-73/script-to-add-a-secondary-group-to-all-users-except-system-default-users-736220/)

Tekken 06-28-2009 11:23 AM

Script to add a secondary group to all users except system default users
 
Hi,

Can any one help me with a script which can add a secondary group to all existing users except system users in linux.

JulianTosh 06-29-2009 03:26 AM

grep ":5[0-9]\{2\}:" /etc/group | sed "s/:$/:newgroup/"

Tekken 06-29-2009 06:15 AM

Hi Admiral,

Thanks for your reply, Could you please post me a shell or Perl script if you have any for this purpose.

JulianTosh 06-29-2009 06:36 AM

Code:

#!/bin/bash

if [ $# -lt 1 ]; then
  echo Description:
  echo  Modifies /etc/group to add specified group to 'normal' users.
  echo  You must redirect output back into /etc/group if you're satisfied
  echo  with the results.
  echo
  echo Usage: $0 [ group ]
  echo
  echo
  exit
fi

sed "s/:\(5[0-9]\{2\}\):/:\1:$1,/" /etc/group

minor fix included.

Tekken 06-29-2009 01:07 PM

Quote:

Originally Posted by Admiral Beotch (Post 3589881)
Code:

#!/bin/bash

if [ $# -lt 1 ]; then
  echo Description:
  echo  Modifies /etc/group to add specified group to 'normal' users.
  echo  You must redirect output back into /etc/group if you're satisfied
  echo  with the results.
  echo
  echo Usage: $0 [ group ]
  echo
  echo
  exit
fi

sed "s/:\(5[0-9]\{2\}\):/:\1:$1,/" /etc/group

minor fix included.

Hi,

I've tried using this by creating a new group and running this script by specifying the group as argument, but i was unable to see the secondary group information for user in the system.I want to create a group and after that by running the script it should make the newly created group as secondary group to all existing users except the system default user.
Could you please help me here....

Thanks in advance

JulianTosh 06-29-2009 04:02 PM

Code:

#!/bin/bash

if [ $# -lt 1 ]; then
  echo Description:
  echo  "Modifies /etc/group to add specified group to 'normal' users."
  echo  "You must redirect output back into /etc/group if you're satisfied"
  echo  "with the results."
  echo
  echo "Usage: $0 [ group ]"
  echo
  echo
  exit
fi

sed "s/:\(5[0-9]\{2\}\):/:\1:$1,/; s/,,/,/g; s/,$//g" /etc/group.new

Sorry I'm not going to do everything for you.

Use the 'groupadd' command to add the group you want. Type 'man groupadd' to figure out how to use it.

To add that new group to all 'normal users', run '<script> NewGroup'. If you like the output, make a backup copy of /etc/group and copy /etc/group.new over /etc/group.

Feel free to ask me about the regular expression used to find the normal users in the group file (if you're interested) and I'll try and explain.


All times are GMT -5. The time now is 10:46 AM.