Operating System - HP-UX
1751712 Members
5562 Online
108781 Solutions
New Discussion юеВ

Script File for /etc/password

 
SOLVED
Go to solution
A Sassi
Advisor

Script File for /etc/password

Hi all,

I am new at scripting and need help??
I need to modify /etc/password file and compare the username to the username in a flat file if it finds it then replace the comments field in /etc/pasword and populated from a flat file with this string Ex. "C-137h 897 Abu Sassi (HP)".

Thanks for your help in Advance
4 REPLIES 4
Steven E. Protter
Exalted Contributor

Re: Script File for /etc/password

The attached script provides you a basis for doing all kinds of things witht he passwd file.

Test it out first on a copy though.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A Sassi
Advisor

Re: Script File for /etc/password

Thanks SEP for your prompt response.
I am actually looking for a script to modify the field in /etc/password file by parsing a flat file.

Thanks

Abu
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Script File for /etc/password

Hi Abu,

You can wait to see other responses. The caveat with chfn function is that it will put blanks with commas for the missing fields. Make sure to *take a backup* of your /etc/passwd file. Also make sure you are only the one working on the system as an administrator during this time. Create a file called 'infile' (changeable) in the following format

user1: C-137h 897 Abu Sassi (HP)
user2: Someone else
user3: bla bla bla

Make sure you don't have a ":" anywhere other than as the seperator itself.

//start of script

#!/usr/bin/ksh
PASSWD=/etc/passwd
MYFILE=infile


while read LINE
do
USER=$(echo $LINE|awk '{FS=":";print $1}')
GECOS=$(echo $LINE|awk '{FS=":";print $2}')

printf "%-20s" "Doing for $USER"
grep -q "^${USER}:" $PASSWD

if [ $? = 0 ]
then
chfn $USER << EOF > /dev/null 2>&1
none
none
none
$GECOS
EOF

printf "- Done\n"

else
printf "- N/A\n"
fi


done < $MYFILE

// end of script

Once the script is run, you can replace ":,,," by editing the passwd file with the command 'vipw' and %s:/:,,,/:/g macro.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
A Sassi
Advisor

Re: Script File for /etc/password

Thanks Sri and sorry for the duplicate post.

I am going to try it and give you my feed back. Your script looks very good. Thanks for all who repond to my post.

Abu