Mirroring a user?

I'm looking for a way to mirror one user to another.

For example:

If I have the user ABC and I want to mirror his:

-Primary group
-Any other groups ABC belongs to
-UID
-Home directory

And create an exact copy over to another server to a user called XYZ. I already have SSH keys set up, don't worry about that, and I have the bulk of the code already .. but..

I'm having trouble writing the bit where it's like:

If

  • the user already exists, just use usermod to change XYZ attributes with all of ABC's info.

else

  • the user doesn't exist, use useradd to create the account XYZ with all of ABC's info.

Here is the bit of code for the usermod:

ssh -q $id@$servera "sudo usermod -d /home/${user1} -g $primgroup -G $grouplist -u $zaquid $user1"
ssh -q $id@$servera "echo "${user}:examplepass" | sudo chpasswd"

How do I write a statement that will execute that whole block of code, or different block of code. Should I put usermod command and the useradd command as a function and just do something like

if x
then usermod
else
useradd

But how do I determine if the user already has an account?

Putting usermod and useradd as functions looks more well arranged to me, and to determine whether the user has an account, execute "grep $user-name /etc/passwd";
for example, if I run this from my box :

grep "^${USERNAME}:" /etc/passwd

would return :

, having in mind that ${USERNAME} is preset within my "env"