I am trying to create a script where it adds a new user and password and also checks to see if that user and password already exist, while running in Root.
So, my script is running and working just fine. It only runs in the root and it correctly checks to see if a username is already being used. But, I cannot seem to add a new user and password. The following is my whole script:
#!/bin/bash
#Creating a script that creates a new user
ROOT_UID=0 #Root has $UID 0
SUCCESS=0
E_USEREXISTS=70
E_NOTROOT=65 #Not root
#Run as root, and this checks to see if the creater is in root. If not, will not run
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Sorry must be in root to run this script"
exit $E_NOTROOT
fi
if [ $# -eq 2 ]; then
username=$1
pass=$2
grep -q "$username" /etc/passwd
if [ $? -eq $SUCCESS ]; then
echo "User $username already exists"
echo "Please choose another username"
exit $E_USEREXISTS
fi
echo $pass | passwd $username --stdin
echo "the account is setup"
else
echo "this program needs 2 arguments and you have given $#"
echo "you have to call the script $0 username and the pass"
fi
exit 0
And I am not getting a straight up error, but here is an example of what is happening: I try to add someone (username then password):
[root@localhost Documents]# ./Script dkong 123bif
And the response is:
passwd: Unknown user name 'dkong'
the account is setup
Could someone help direct me? I want it to create a new username and password and I am not understanding why it is not.
It has been a while since I have used scripts so I am sorry if the answer is obvious! Just need a little direction. Thank you in advance!