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
# 8  
Old 01-16-2009
Clearly, I've not been my best on this problem. After further testing, I found another bug.
Code:
for i in `awk -F: '$1 == "'"$HOSTNAME"'" && $2 == '"${i}"' { print $2 }' $FILE1`

should be:
Code:
for i in `awk -F: '$1 == "'"$HOSTNAME"'" { print $2 }' $FILE1`

Then later, I have exit(0) in an awk. The problem is, the exit(1) in the END{ } block will get executed regardless. To avoid this, the exit must specify a latched value. So the proper line should be:
Code:
if awk -F: '$1 == "'"${i}"'" { found=1; exit; } END { exit(!found);}' $FILE2

Then I noticed that sometimes the username is listed twice in update.txt, so that the FILED1=`...` ends up with multiple lines of output. Also, in case the comment varies by hostname, the awk should be more selective. Here's the fix for that:
Code:
FIELD1=`awk -F: '$1 == "'"$HOSTNAME"'" && $2 == "'"${i}"'" { print $3;exit; }' $FILE1`

I have edited the original post respectively.
# 9  
Old 01-23-2009
Hi there,

The script is still not working it just execute and does nothing to /etc/passwd and it gives no errors this time. Please check below I have attached my /etc/passwd output before executing the script then the content of the script and output produced after execution check /etc/passwd work nothing has been updated. I actually tested it on two servers which are server1 and server2 same output nothing updated.

ntp:x:38:38::/etc/ntp:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
distcache:x:94:94Smilieistcache:/:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
student2:x:500:500:student2:/home/student2:/bin/bash
web:x:100:48:Web File Owner:/home/web:/bin/bash
SINLO_R:x:501:501::/home/SINLO_R:/bin/bash
AVNLNX:x:502:502::/home/AVNLNX:/bin/bash
REGGIE_P:x:503:503::/home/REGGIE_P:/bin/bash
STONE_P:x:504:504::/home/STONE_P:/bin/bash
GCOGCO:x:505:505::/home/GCOGCO:/bin/bash
NDUMIE:x:506:506::/home/NDUMIE:/bin/bash
MANGI:x:507:507::/home/MANGI:/bin/bash
MZIZA_G:x:508:508::/home/MZIZA_G:/bin/bash
[root@server2 script]# date
Thu Jan 22 21:49:49 SAST 2009

#!/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
~
~
[root@server2 script]# ls
unixscript.txt
[root@server2 script]# ./unixscript.txt
[root@server2 script]#

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
pcap:x:77:77::/var/arpwatch:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
apache:x:48:48:Apache:/var/www:/sbin/nologin
distcache:x:94:94Smilieistcache:/:/sbin/nologin
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
dovecot:x:97:97:dovecot:/usr/libexec/dovecot:/sbin/nologin
webalizer:x:67:67:Webalizer:/var/www/usage:/sbin/nologin
squid:x:23:23::/var/spool/squid:/sbin/nologin
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
student2:x:500:500:student2:/home/student2:/bin/bash
web:x:100:48:Web File Owner:/home/web:/bin/bash
SINLO_R:x:501:501::/home/SINLO_R:/bin/bash
AVNLNX:x:502:502::/home/AVNLNX:/bin/bash
REGGIE_P:x:503:503::/home/REGGIE_P:/bin/bash
STONE_P:x:504:504::/home/STONE_P:/bin/bash
GCOGCO:x:505:505::/home/GCOGCO:/bin/bash
NDUMIE:x:506:506::/home/NDUMIE:/bin/bash
MANGI:x:507:507::/home/MANGI:/bin/bash
MZIZA_G:x:508:508::/home/MZIZA_G:/bin/bash
[root@server2 script]# date
Thu Jan 22 21:52:19 SAST 2009
[root@server2 script]#

Thanks
# 10  
Old 01-23-2009
When it looks like it is outputting the right commands, remove the "TEST=echo" line. This was in my original post. Smilie
# 11  
Old 01-26-2009
Hi Otheus,

Do you by any chances have a test machine will you kindly show me the out of your script after running it. I have removet the test=echo and excecuted the script nothing happened no output was produced and the /etc/passwd stayed the same. I then removed the variable $test on the body but still nothing happened.

Thanks
The Duke
# 12  
Old 01-26-2009
No, I'm sorry; I don't have a test machine for you. Smilie The script should produce no output in non-test mode. That's unsurprising. I'm not sure why /etc/passwd stayed the same, however. Put the test/echo back in and post the output. Maybe I'll spot another error.
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