Using GID,Secondary ID in useradd command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using GID,Secondary ID in useradd command
# 1  
Old 07-01-2012
Using GID,Secondary ID in useradd command

Hi All,

I am writing the script for user creation and want to use GID and Secondary group id from previous similar profile.Any one help me how we will take out GID & Secondary and use in useradd command.

Code:
$ id -a test
uid=6236(test) gid=999(local) groups=999(local),9003(testadmin)

Above id -a i want to use use below command groupiD=999 & secondory_group=9003.

sudo /usr/sbin/useradd -u $userID -g $groupiD -G $secondory_group -c "$fullname" -m -d $HomeDirectory -s $shell $login

Thanks,
Susi.S
# 2  
Old 07-02-2012
Are you trying to extract values from the output of id -a .. ?? If yes, try the below..
Code:
groupiD=$(id -a | tr ' ' '\n' | nawk -F"[(),=]" '/gid=/{print $2}')
secondary_group=$(id -a | tr ' ' '\n' | nawk -F"[(),]" '/groups=/{print $4}')

# 3  
Old 07-02-2012
Hi Jayan,

Thanks for update..As per your command is working if secondary group is present and some of user not having secondary ID.Please help me how it will work if secondary id not present.We have to put any looping?

Ex :-

Code:
$ id -a kmanoh
uid=5573(kmanoh) gid=2022(dbaprod) groups=2022(dbaprod)
$

Above user not having secondary ID so we have to use 2022 is alone..

# 4  
Old 07-02-2012
Try this for extracting secondary group id ..
Code:
$ secondary_group=$(id -a | tr ' ' '\n' | nawk -F"[(),=]" '/groups=/ {if($0~/\,/){print $5}else{print $2}}')

# 5  
Old 07-02-2012
Hi Jay,

Thank a lot for your help.But one server is working and other server is not working..

Code:
Working :-
$ id -a oracle
uid=7003(oracle) gid=2002(dba) groups=2002(dba)

$ id -a oracle | tr ' ' '\n' |awk -F"[(),=]" '/groups=/ {if($0~/\,/){print $5}else{print $2}}'
2002
$ more /etc/redhat-release
Red Hat Enterprise Linux AS release 4 (Nahant Update 6)
$

Not Working server :-

$ id -a oracle
uid=2006(oracle) gid=2006(dba) groups=2006(dba)
$ id -a oracle | tr ' ' '\n' |awk -F"[(),=]" '/groups=/ {if($0~/\,/){print $5}else{print $2}}'

$ id -a oracle | tr ' ' '\n'
uid=2006(oracle)
gid=2006(dba)
groups=2006(dba)
$ uname -a
SunOS 5.10 Generic_147440-06 sun4u sparc SUNW,Sun-Fire-280R
$

Cheers,
Susi.S

Last edited by susindram; 07-02-2012 at 11:31 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with useradd command

Hi, I want to create multiple user in same home directory. Is it possible ? If possible then please tell me how. Thanks in advance. (3 Replies)
Discussion started by: mnmonu
3 Replies

2. UNIX for Advanced & Expert Users

Numeric uid and gid in ls -l command

I´m listing the contents of a directory using the command ls -lI get numeric uid and gid for some lines. example: drwxr-xr-x root root 1970-01-01 01:00 sys -rw-r--r-- 501 20 0 2010-08-04 14:54 shutdown.bravo.rc drwxr-x--- 501 20 ... (5 Replies)
Discussion started by: flocki
5 Replies

3. UNIX for Advanced & Expert Users

GID mappings cached?

We had a mapping to one UNIX box just fine, the server was changed to another UNIX box and now when mapping drives from Windows box to new UNIX box, we get the old GID associated with our Windows user. When checking on new UNIX box, we see GID of 108, which is what we want, but when we map the... (0 Replies)
Discussion started by: bd4021
0 Replies

4. UNIX for Dummies Questions & Answers

Get the gid from a group name

Hi there, One can easily retrieve the uid from a username : id -u $username But how can we easily retrieve the gid from a group name? I mean is there a command without using sed? sed -nr "s/^$groupname:x:(+):.*/\1/p" /etc/group Thanks for your help Santiago (7 Replies)
Discussion started by: chebarbudo
7 Replies

5. Solaris

Purpose D option in useradd command

Hi Can any one tell about -D option in the useradd command? Thanks MaroV (3 Replies)
Discussion started by: vr_mari
3 Replies

6. UNIX for Advanced & Expert Users

How get secondary ID. ??

This is one our office BOX Sun Microsystems Inc. SunOS 5.10 Generic January 2005 Kisses% rlogin pebblz01 -l adminID Password: Last login: Sat Feb 14 01:11:36 from Kisses Please enter your own login id and password. Your login: I_rule Your password:xxxxx From the above... (3 Replies)
Discussion started by: dashok.83
3 Replies

7. UNIX for Dummies Questions & Answers

useradd command

what if useradd ? or useradd * is given as input???? if it wont execute. then what are the values for name shud be avoided like ? * (1 Reply)
Discussion started by: Gopi Krishna P
1 Replies

8. UNIX for Advanced & Expert Users

To get PID of a command passed to a secondary system

Hi Reqmt: I am working on Sys 1 and passes a command to Sys2 using a utility which has ssh coded in it. Is there any way to get the process id of the command in Sys 2 from my Sys1 ? Note: The utility is not editable, so you cannot get it through it and I am not logged in in Sys2. Thanks... (2 Replies)
Discussion started by: anu_math9
2 Replies

9. UNIX for Dummies Questions & Answers

Who can help me how to add user without 'useradd' command

Can any one tell me if i dun want using 'useradd' command to add new user, it is have any command i can use to add a new user?and any extra step needed for my local environment? (3 Replies)
Discussion started by: foong
3 Replies

10. UNIX for Dummies Questions & Answers

Want to create new users without using "useradd" command and user Interaction.

Don't want to use useradd command to create a new user I am doing manual addition of accounts. Actually I am writing a script and I am working on RedHat7.1 system. What I am trying to do is run the script and pass all the values of the username password and group as command line parameters. Now... (2 Replies)
Discussion started by: syedifti
2 Replies
Login or Register to Ask a Question