Append users if GID exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append users if GID exists
# 1  
Old 09-27-2012
Append users if GID exists

I have thousands of users assigned various roles. The role header defines the GID that they should have. If I can get all the GID information into /etc/group format I can upload the output file and easily assign all users into the desired GID. Right now I can get an output file like this:
Code:
oracle:x:4579:user1
oracle:x:4579:user2
sybase:x:9475:user1
sybase:x:9475:user2
oracle:x:4579:user3
oracle:x:4579:user4
sybase:x:9475:user5

There's nothing wrong with uploading the file like this, but it's pretty big. The way I produce this is via the following function:
Code:
for i in $(cat $tmp_gid_file)
do
TMP_GID=`grep -w $i master_group_list | sed 's/:/:x:/'`
echo $TMP_GID:$USER_ID >> gid_file

tmp_gid_file has a list of GID's line by line. I have to search the GID in a master file instead of getent group (it's a long story) and it returns <group_name>:<GID> so that's why I have to replace the colon with a :x:

I'd like to produce a GID file that has only 1 GID entry per line and does not duplicate. I would like it to look like this:
Code:
oracle:x:4579:user1,user2,user3,user4
sybase:x:9475:user1,user2,user5

I started with an if statement but I have a problem appending the user's ID to the end of the line where the GID is found:
Code:
GID_EXISTS=`grep -iw $i gid_file`
if [ ! -z $GID_EXISTS ] 
then
   sed '/$i/s/$/:$USER_ID/' gid_file  ##This seems to print the line, not append 
                                      ##the user's ID to the line where the GID is found
else
...

I'm using GNU/Linux (that's what I get from uname -a...not sure if it's rhel or suse or what) and my shell is bash.

Last edited by vbe; 09-27-2012 at 05:27 AM..
# 2  
Old 10-05-2012
It sounds like you could egrep or sed or awk or bash a list as it downloads, just keeping what you want for a list of users.

$Variables are not evaluated inside single quotes (not much is, so I prefer them, generally).

No point in going through the file twice, with sed and grep.

Red hat exact identity is in /etc/*red*hat* !
# 3  
Old 10-05-2012
You could use something like the following:

Code:
awk -F':' '{ idx=$1":"$2":"$3":"; rec[idx]=rec[idx]","$4 } END {for (var in rec) print var substr(rec[var],2) }' YOURGIDFILE

# 4  
Old 10-09-2012
If you can decompose the installed user ids and each gid into lines in a simple file, and do the same for the desired user ids and gid, then you can sort them and pass them to comm to find out what is missing. On bash, and ksh on systems with /dev/fd/[0-9]*, you can pipe it all and have less clutter and selay:
Code:
comm -23 <(
  gen-desired-list-to-stdout | sort -u
 ) <(
  gen-actual-list-to-stdout | sort -u
 )| create-any-missing-group-or-missing-id-or-id-group-affiliation

# 5  
Old 10-12-2012
I figured it out before I was able to consider some of your suggestions Smilie I grep the destination file to see if the GID already exists in the file that I store that in a variable. If that variable is empty then I append the GID definition to the bulkload file. If the variable is not null then I remove the line that contains the GID definition and then replace it with the same thing but with the user's id appended at the end.

Probably not very efficient as DGPickett points out...I may have to try a different logic as we increase in numbers.
# 6  
Old 10-15-2012
Yes, it is nice to avoid n^2 problems at the start! Solutions with comm are very robust and scale well, can exploit multiprocessing and such well, detect both negative (missing) and positive (wrong) problems!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Display info about users (UID GID processes terminal)

I would like to get an opinion for my solution for this task and get feedback about better approach or mistakes I have made. 1. The problem statement, all variables and given/known data: The task is to create a script which prints information about users whose names are specified in the... (2 Replies)
Discussion started by: kornfan
2 Replies

2. Shell Programming and Scripting

Find users with root UID or GID or root home

I need to list users in /etc/passwd with root's GID or UID or /root as home directory If we have these entries in /etc/passwd root:x:0:0:root:/root:/bin/bash rootgooduser1:x:100:100::/home/gooduser1:/bin/bash baduser1:x:0:300::/home/baduser1:/bin/bash... (6 Replies)
Discussion started by: anil510
6 Replies

3. Shell Programming and Scripting

File exists, but cannot be opened.How to check- whether it could be opened to read when it exists

Hi #Testing for file existence if ; then echo 'SCHOOL data is available for processing' else echo 'SCHOOL DATA IS NOT AVAILABLE FOR PROCESSING' : i wrote a script, where it begins by checking if file exists or not. If it exists, it truncates the database... (2 Replies)
Discussion started by: rxg
2 Replies

4. UNIX for Dummies Questions & Answers

uid ,gid value change

Present /home/dsadm# id dsadm uid=0(root) gid=0(root) ---------------------------------- needs to be /home/dsadm> id dsadm uid=23186(dsadm) gid=16284(gdstage) Please provide the command/steps for the above uid, gid value change Thanks in advance for all your support . ... (3 Replies)
Discussion started by: sridhardwh
3 Replies

5. 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

6. AIX

script for finding all the users with GID 0 ( admin group )

Hi Friends, I am trying to write a script for finding all the users with the GID 0 i.e. Admin users. can you please help me on this. (1 Reply)
Discussion started by: anoopraok
1 Replies

7. Shell Programming and Scripting

find, if exists then append for file with same name

I will have to process multiple files with same name everyday. My requirement is: If on a certain day I see that filename.txt exists then the contents of the filename.txt would be added/append to the former file contents.Each time it sees the file the content would be added.But the header ... (8 Replies)
Discussion started by: RubinPat
8 Replies

8. UNIX for Dummies Questions & Answers

rm: Unable to remove directory /mnt/users/test/logs/: File exists

rm: Unable to remove directory /mnt/users/test/logs/: File exists ls -latr total 191208 drwxrwxrwx 6 test echo 4096 Jul 3 22:36 .. -rwxrwxrwx 1 test echo 97692804 Jul 3 22:36 .nfsDFA4 drwxrwxr-x 2 test echo 4096 Jul 3 23:00 . M not able to delete... (4 Replies)
Discussion started by: solitare123
4 Replies

9. UNIX for Advanced & Expert Users

GID & UID for HP-UX ?

Hi, can somebody tell me how to make directoriy in which all files will be generated as dir owner ? I'm able to add GID for directory group and files have group id like folder , but when i do same for user files still are created as user which creates them :( drwsrwsrwx 2 flexbul ... (2 Replies)
Discussion started by: pp56825
2 Replies

10. Shell Programming and Scripting

append 3 users in /etc/passwd

I am looking to add 3 lines in /etc/passwd via a script. Can you please give me an idea on how to write a script that can do that? (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question