Updating the comments field on /etc/passwd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating the comments field on /etc/passwd
# 1  
Old 01-12-2009
Question Updating the comments field on /etc/passwd

Hi there,

I have more that 300 servers that I need to updated the comments field on /etc/passwd for users that have a blank comments fields. The users have accounts on different servers. I have created a list of these users on a text file called update.txt check below.
I need a script that will compare this file with /etc/passwd first backup the passwd database and update the comment field on /etc/passwd with the details on the text file if it's blank.
I have a script that I have created not sure if it will do the job as I have not worked a lot with scripts.
Below is my script and attached is my text file:
Code:
#!/usr/bin/bash
FILE1=/tmp/update.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.orig

for i in `cat $FILE1 | awk -F":" '{ print $2 }'`
do
 FIELD1=`cat $FILE1 | grep ${i} |awk -F":" '{ print $3 }'`
 grep $i ${FILE2}
 if [ $? -eq 0 ]
 then
  usermod -c "${FIELD1}" $i
 fi
done

I will highly appreciate your assistance.

Update.txt

SERVER1:SINLO_R:SINLO ROGGER
SERVER1:AVNlnx:LINUX Admistrator/junior
SERVER1:nfsnobody:LINUX Admistrator
SERVER1:REGGIE_P:REGGIE PABLO
SERVER1:STONE_P:STONE PANE
SERVER2:SINLO_R:SINLO ROGGER
SERVER2:AVNLNX:LINUX Admistrator/junior
SERVER2:nfsnobody:LINUX Admistrator
SERVER2:REGGIE_P:REGGIE PABLO
SERVER2:STONE_P:STONE PANE

Last edited by otheus; 01-12-2009 at 10:36 AM.. Reason: [code] tags and formatting
# 2  
Old 01-12-2009
First, overall, the script will probably work. Good job.

Second, using the usermod command is cool, and does a lot of the work for you. Make sure you manually back up the /etc/passwd file to a unique file. Otherwise every time you run the script (ie, fix a bug), the original backup will be removed. Just add "$$" to the backup file, and that will probably do the job.

Third, it's not necessary to cat | awk , nor to cat | grep | awk. If that helps you understand it better, fine. In both cases, awk will work on the file you give it. Awk will also act like grep. So you could do:
Code:
FIELD1=`awk -F: '$2 == "'"${i}"'" { print $3;exit; }' $FILE1`

Note, there are single quotes followed by double-quotes and vice versa. These make sure that the shell "sees" the variable you want it to see, but not the ones you want awk to see.

Searching by field makes sure that "SINLO" does not match both "SINLO" and "SINLO_R" and "ASINLO", for instance.

Similarly, I would change the "grep $FILE2" to:
Code:
awk -F: '$1 == "'"${i}"'" { found=1;exit; } END { exit(!found);}' $FILE2

This does the same thing as your grep command, but matches by field and exits with "true" as soon as a match is found. Otherwise, it exits with "false".

Third, you don't need to get FIELD1 until you know there's a matching entry in the passwd file. So put that line in the if-then condition.

Finally, to make sure your username matches the current server, you change the awk in the for loop to:
Code:
for i in `awk -F: '$1 == "'"$HOSTNAME"'" && $2 == '"${i}"' { print $2 }' $FILE1`

Again, double- and single-quoting needed like this.

Putting it all together we have:
Code:
#!/usr/bin/bash
FILE1=/tmp/update.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.$$.orig
TEST=echo

for i in `awk -F: '$1 == "'"$HOSTNAME"'" { print $2 }' $FILE1`
do
 if awk -F: '$1 == "'"${i}"'" { found=1; exit; } END { exit(!found);}' $FILE2
 then
   FIELD1=`awk -F: '$1 == "'"$HOSTNAME"'" && $2 == "'"${i}"'" { print $3;exit; }' $FILE1`
  $TEST usermod -c "${FIELD1}" $i
 fi
done

When the output shows the correct usermod commands, you are ready to go. Just change "TEST=echo" to "TEST=".

Last edited by otheus; 01-16-2009 at 04:21 AM.. Reason: Latest fix: loop variable should be i, not f
# 3  
Old 01-12-2009
I have executed the script it backs up the /etc/passwd file fine and it does increment it if you excecute the script twice. The script the gives a syntax error with the following out put below. When I check the /etc/passwd file none of the comments field have been update.

Error:
[root@server1 script]# ls
scriptgugu.txt workingscript.txt
[root@server1 script]# ./workingscript.txt
awk: $1 == server1.localdomain && $2 == { print $2 }
awk: ^ syntax error
awk: $1 == server1.localdomain && $2 == { print $2 }
awk: ^ syntax error
[root@server1 script]#
# 4  
Old 01-13-2009
Sorry! I fixed the original message to double-quote all strings within awk. Hopefully that solves the problem.
# 5  
Old 01-14-2009
Howzit Otheus,

I updated the script and I'm still getting syntax errors please have a look at the out put below: My prayers are not getting answered!!!!!!!!!!

#!/bin/bash
FILE1=/tmp/update.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.$$.orig
TEST=echo

for i in `awk -F: '$1 == "'"$HOSTNAME"'" && $2 == '"${i}"' { print $2 }' $FILE1`
do
if awk -F: '$1 == "'"${i}"'" { exit(0); } END { exit(1);}' $FILE2
then
FIELD1=`awk -F: '$2 == "'"${i}"'" { print $3 }' $FILE1`
$TEST usermod -c "${FIELD1}" $i
fi
done
~
~
[root@server1 script]# ./scriptlinuxcom.txt
awk: $1 == "server1.localdomain" && $2 == { print $2 }
awk: ^ syntax error
[root@server1 script]#

Last edited by otheus; 01-15-2009 at 05:50 AM..
# 6  
Old 01-15-2009
Ooops. The variable "f" should have been "i".
# 7  
Old 01-15-2009
I have updated the script but it's still not working please have a look below.

#!/bin/bash
FILE1=/tmp/update.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.$$.orig
TEST=echo

for i in `awk -F: '$1 == "'"$HOSTNAME"'" && $2 == '"${i}"' { print $2 }' $FILE1`
do
if awk -F: '$1 == "'"${i}"'" { exit(0); } END { exit(1);}' $FILE2
then
FIELD1=`awk -F: '$2 == "'"${i}"'" { print $3 }' $FILE1`
$TEST usermod -c "${FIELD1}" $i
fi
done
~
[root@server1 script]# ./scriptlinuxcom.txt
awk: $1 == "server1.localdomain" && $2 == { print $2 }
awk: ^ syntax error
[root@server1 script]#

I'm still getting syntax error's when executing the script.If you look below at the last six users
my /etc/password comment field has not been updated with the comments from the /tmp/update.txt.

/etc/passwd

sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
student1:x:500:500:student1:/home/student1:/bin/bash
web:x:100:48:Web File Owner:/home/web:/bin/bash
SINLO_R:x:501:501:SINLO ROGGER :/home/SINLO_R:/bin/bash
AVNlnx:x:502:502:LINUX Admistrator/junior:/home/AVNlnx:/bin/bash
REGGIE_P:x:503:503:REGGIE PABLO:/home/REGGIE_P:/bin/bash
STONE_P:x:504:504:STONE PANE:/home/STONE_P:/bin/bash
SINLOR:x:505:505:SINLO ROGGER :/home/SINLOR:/bin/bash
GUGU_M:x:506:506::/home/GUGU_M:/bin/bash
SANDISA:x:507:507::/home/SANDISA:/bin/bash
GCOGCO:x:508:508::/home/GCOGCO:/bin/bash
NDUMIE:x:509:509::/home/NDUMIE:/bin/bash
MANGI:x:510:510::/home/MANGI:/bin/bash
MZIZA_G:x:511:511::/home/MZIZA_G:/bin/bash

/tmp/update.txt

[root@server1 etc]# cat /tmp/update.txt
SERVER1:SINLO_R:SINLO ROGGER
SERVER1:AVNlnx:LINUX Admistrator/junior
SERVER1:nfsnobody:LINUX Admistrator
SERVER1:REGGIE_P:REGGIE PABLO
SERVER1:STONE_P:STONE PANE
SERVER1:GUGU_M:LINUX ENGINEER
SERVER1:SANDISA:SYSTEM ADMIN
SERVER1:GCOGCO:MTN OPERATOR
SERVER1:NDUMIE:MTN OPERATOR
SERVER1:MANGISmilieBA ORACLE
SERVER1:MZIZA_G:SPECIALIST ORACLE
SERVER2:SINLO_R:SINLO ROGGER
SERVER2:AVNLNX:LINUX Admistrator/junior
SERVER2:nfsnobody:LINUX Admistrator
SERVER2:REGGIE_P:REGGIE PABLO

The is another script that was created for me this script worked fine but because it was not using usermod it
updated the /etc/shadow id and then this made users when loggining on appear with out user name's.

This is the script:

FILE1=/tmp/update.txt
FILE2=/etc/passwd
cp $FILE2 /etc/passwd.orig

awk 'BEGIN{FS=OFS=":"} NR==FNR{a[$2]=$3;next}
$1 in a && $5==""{$5=a[$1]} {print}' "$FILE1" "$FILE2" > passwd_new

mv passwd_new $FILE2
~
~
[root@server1 script]# ./scriptgugu.txt
[root@server1 script]#

If you now look at the /etc/passwd the comment field has been updated but now when I loggon to the system
with the users I have problems of them logging in but after that the command prompt appears with out a username.
e.g. ReGGIE_P will logon to the system but the prompt will have " I don't have a username linked to this id 435$ "
This created problems for me as some of the accounts a used to run applications.

This is the out put of /etc/passwd asfter executing the scrip:

gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
student1:x:500:500:student1:/home/student1:/bin/bash
web:x:100:48:Web File Owner:/home/web:/bin/bash
SINLO_R:x:501:501:SINLO ROGGER :/home/SINLO_R:/bin/bash
AVNlnx:x:502:502:LINUX Admistrator/junior:/home/AVNlnx:/bin/bash
REGGIE_P:x:503:503:REGGIE PABLO:/home/REGGIE_P:/bin/bash
STONE_P:x:504:504:STONE PANE:/home/STONE_P:/bin/bash
SINLOR:x:505:505:SINLO ROGGER :/home/SINLOR:/bin/bash
GUGU_M:x:506:506:LINUX ENGINEER:/home/GUGU_M:/bin/bash
SANDISA:x:507:507:SYSTEM ADMIN:/home/SANDISA:/bin/bash
GCOGCO:x:508:508:MTN OPERATOR:/home/GCOGCO:/bin/bash
NDUMIE:x:509:509:MTN OPERATOR:/home/NDUMIE:/bin/bash
MANGI:x:510:510SmilieBA ORACLE:/home/MANGI:/bin/bash
MZIZA_G:x:511:511:SPECIALIST ORACLE:/home/MZIZA_G:/bin/bash

look at the last six fields the comment field has been updated.

Please asssist I hope this can give u an idea of the problem.

Thanks for all the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Passwd -l or -u modifies lastchg field in /etc/shadow file

Hi, I have a Solaris 10 box where password aging is not functioning properly. Using the passwd command with the -l or -u options causes the lastchg field in the /etc/shadow file to be modified. Therefore, if a user's password is set to expire in 90 days and they are 1 day away, all they have... (4 Replies)
Discussion started by: cschar
4 Replies

2. AIX

When did AIX start using /etc/security/passwd instead of /etc/passwd to store encrypted passwords?

Does anyone know when AIX started using /etc/security/passwd instead of /etc/passwd to store encrypted passwords? (1 Reply)
Discussion started by: Anne Neville
1 Replies

3. UNIX for Dummies Questions & Answers

Delete Comments

Hello i am back :D, i have a prolem. I want to Delete the IPs which are in Comments. Input 192.168.0.1 192.168.0.2 #192.168.0.3 #192.168.0.4 - when TAB or Space, delete too. /*192.168.0.5 192.168.0.6 192.168.0.7*\ Output 192.168.0.1 192.168.0.2 My solution is sed -e... (7 Replies)
Discussion started by: eightball
7 Replies

4. Solaris

passwd cmd reenables passwd aging in shadow entry

Hi Folks, I have Solaris 10, latest release. We have passwd aging set in /etc/defalut/passwd. I have an account that passwd should never expire. Acheived by emptying associated users shadow file entries for passwd aging. When I reset the users passwd using passwd command, it re enables... (3 Replies)
Discussion started by: BG_JrAdmin
3 Replies

5. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

6. UNIX for Dummies Questions & Answers

Updating a field in a File without creating temp file's

Hi Experts, I have a requirement where i need to update the below items in file, 1. END TIME 2. PREV_STATUS For the first time the PREV_status and end time of all job the job will be sysdate & NULL reply as below, Session_name,Load Type,Frequency,Seesion End time,Prev_Status... (2 Replies)
Discussion started by: prabhutkl
2 Replies

7. Shell Programming and Scripting

Script for updating the comments field on /etc/passwd on redhat linux

Hi there, I have more that 300 servers that I need to updated the comments field on /etc/passwd for users that have a blank comments fields. The users have accounts on different servers. I have created a list of these users on a text file called update_passwd.txt. I need a script that will... (6 Replies)
Discussion started by: Linux Duke
6 Replies

8. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

9. Programming

lint comments

Hi can anyone help me regarding the meaning of the following lint messages. what is the use of having such lint comments in the c program. /*lint -esym(534,cputs,fgets,cprintf) */ /*lint -efile(766,pragmas.h) */ Thanks a lot in advance. (5 Replies)
Discussion started by: axes
5 Replies

10. UNIX for Dummies Questions & Answers

Trying to extract a field from /etc/passwd file..

Hello, was looking for some help on extracting a field from the passwd file. So far I have made a copy of the passwd file and changed my rights so I can edit it. Every user's password is coded as an :x:, and my goal was to change that x to a blank, and then try to extract any user with that field... (2 Replies)
Discussion started by: xBuRnTx
2 Replies
Login or Register to Ask a Question