Public paste
Undefined
By: Guest | Date: Jun 10 2015 12:05 | Format: None | Expires: never | Size: 551 B | Hits: 447

  1. #!/bin/bash
  2. # Script to add a user to Linux system
  3. if [ $(id -u) -eq 0 ]; then
  4.         read -p "Enter username : " username
  5.         read -s -p "Enter password : " password
  6.         egrep "^$username" /etc/passwd >/dev/null
  7.         if [ $? -eq 0 ]; then
  8.                 echo "$username exists!"
  9.                 exit 1
  10.         else
  11.                 pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
  12.                 useradd -m -p $pass $username
  13.                 [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
  14.         fi
  15. else
  16.         echo "Only root may add a user to the system"
  17.         exit 2
  18. fi