Monday, December 28, 2020

how to add user using shell script?

 #!/bin/bash -x 


if [ $(id -u) -eq 0 ]; then

read -p "Enter username : " username

read -s -p "Enter password : " password

egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then

echo "$username exists!"

exit 1

else

if [ -f /usr/bin/mkpasswd ]; then


pass=$(mkpasswd -m sha-512 $password)

#mkpasswd will create encrypted password.

useradd -m -p "$pass" "$username"

[ $? -eq 0 ] && echo "User has been added to system!" && chage -d0 $username  || echo "Failed to add a user!"

else

echo "install package apt-get install whois"

       fi

fi

else

echo "Only root may add a user to the system."

exit 2

fi

No comments:

Post a Comment