User account shell script

shell script to create user accounts from a text file

Page 1 of 1

0 Replies - 2123 Views - Last Post: 21 February 2010 - 08:23 PM Rate Topic: -----

#1 snakeshite   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-November 09

User account shell script

Posted 21 February 2010 - 08:23 PM

Can someone tell me what's wrong with my shellscript?
People telling me to use gawk instead of grep.

In which write a shell script to create user accounts from a text file. The format of the text file is: accountname emailaddress. The script will search the /etc/passwd file to verify the account name is NOT used. If the account name exists, print a message to an error log.
If the account name does not exist; randomly generate a password, create the account and email the account information to the new user’s email address. The script should accept only one argument (positional parameter), the name of a text file. Remember to test if the file exists.

# February 21, 2010
# Shell scripting
# shell script to create User accounts from a text file.

HOME_BASE = "/home/"
#checks if textfile exists
if [ $# = 0 ]
echo "USAGE ./user.bash FILENAME"
exit 1
elif [ ! -f "$1" ]
echo "$1 doesn't exist."
exit 1
cat ${NEW_USERS} | \
while read USER EMAIL
# checks if USER exists
grep "^${USER}:" /etc/passwd > /dev/null 2>&1
if [ "$?" -ne "0" ]
useradd -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
# email subject
# Email text/message
echo "This is your username: $USER"> $EMAILMESSAGE
echo "This is password: $PASSWORD" >>$EMAILMESSAGE
# send an email using mutt
rm /tmp/emailmessage.txt
# print to error log if username exists
echo " This user $USER already exists" >> log


This post has been edited by snakeshite: 21 February 2010 - 08:23 PM

Is This A Good Question/Topic? 0
  • +

Page 1 of 1