Sponsored Content
Top Forums Shell Programming and Scripting Updating the comments field on /etc/passwd Post 302275842 by otheus on Monday 12th of January 2009 10:01:13 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
GROUP(5)						      BSD File Formats Manual							  GROUP(5)

NAME
group -- format of the group permissions file DESCRIPTION
The file </etc/group> consists of newline separated ASCII records, one per group, containing four colon ':' separated fields. These fields are as follows: group Name of the group. passwd Group's encrypted password. gid The group's decimal ID. member Group members. The group field is the group name used for granting file access to users who are members of the group. The gid field is the number associ- ated with the group name. They should both be unique across the system (and often across a group of systems) since they control file access. The passwd field is an optional encrypted password. This field is rarely used and an asterisk is normally placed in it rather than leaving it blank. The member field contains the names of users granted the privileges of group. The member names are separated by commas without spaces or newlines. A user is automatically in a group if that group was specified in their /etc/passwd entry and does not need to be added to that group in the /etc/group file. INTERACTION WITH DIRECTORY SERVICES
Processes generally find group records using one of the getgrent(3) family of functions. On Mac OS X, these functions interact with the DirectoryService(8) daemon, which reads the /etc/group file as well as searching other directory information services to determine groups and group membership. FILES
/etc/group SEE ALSO
passwd(1), setgroups(2), crypt(3), getgrent(3), initgroups(3), passwd(5), DirectoryService(8) BUGS
The passwd(1) command does not change the group passwords. HISTORY
A group file format appeared in Version 6 AT&T UNIX. Mac OS X July 18, 1995 Mac OS X
All times are GMT -4. The time now is 04:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy