User account shell script

shell script to create user accounts from a text file

Page 1 of 1

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

#1 snakeshite  Icon 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.

/code
#!/bin/bash
# February 21, 2010
# Shell scripting
# shell script to create User accounts from a text file.

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

/code

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


Is This A Good Question/Topic? 0
  • +

Page 1 of 1