script to modify /etc/shadow

Hi

I am looking for script to modify /etc/shadow.

For example:
1-)User enters username
2-)The line of that user is found in /etc/shadow and *LK* is added infront of the second field in /etc/shadow.

How can I do this?

Thanks

nawk -v user="$user" 'BEGIN{FS=":"}
{
if($1==user)
{
  $2=*LK*
  print
}' /etc/shadow

And here is the sed solution:

sed -i "/$user/s/\([^:]*:\)\{1\}/&*LK/" /etc/shadow

You can build the script from here.

the nawk solutions need to be slightly modified. the requirements asked for a *LK* to be prepended to the second field - not replace the entire field.

the sed solution should use ^$user: for the pattern to ensure that only one account gets modified.

thanks a lot, I will work on them

one last thing;
after user enters username
I want to check the username with the usernames in the /etc/shadow.
If the entered username doesnt exist, I will return error,
else I will lock the account with
sed -i "/$user/s/\([^:]*:\)\{1\}/&*LK/" /etc/shadow

How can I do that?

This should work.

test $(grep -c $user /etc/shadow) -gt 0 && sed -i "/$user/s/\([^:]*:\)\{1\}/&*LK/" /etc/shadow || echo ERROR

I assume you know this but in case you don't there are utilities that already do this for you so you don't need to reinvent the wheel.

passwd
usermod
etc..