View Single Post

  #4 (permalink)  
Old 07-13-2006, 06:20 PM
tom tom is offline
Contributors
User
 
Join Date: Jun 2005
Location: London, UK
Posts: 213
Rep Power: 4
tom is on a distinguished road
Default

Here is code:

Code:
#!/bin/bash
UP="/tmp/upass"
UN="/tmp/uname"

function getUser(){
dialog --backtitle "HOV Services" --title "Add User" --inputbox "Enter the Username" 10 40 2>$UN
STAT=$?
case $STAT in
1) rm -f $UN ; return ;;
255) rm -f $UN ; return ;;
esac 
}

function getPassword(){
dialog --backtitle "HOV Services" --title "Add User" --inputbox "Enter the Password" 10 40 2>$UP
STAT=$?
case $STAT in
1) rm -f $UP ; return ;;
255) rm -f $UP ; return ;;
esac
}

function addUsertoSystem(){
user="$1"

# make sure user and password are supplied
if [ "$user" == ""  -o  ! -f $2  ]; then
dialog --backtitle "HOV Services" --title "Error" --yesno "Username or password is not given!\nDo you want to try again?" 7 60
    if [ $? -eq 0 ]; then
           getUser
           getPassword
    else
           echo "Bye!"
           exit 1
    fi
fi

# make sure user does not exist
grep -E "^$user" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then
dialog --backtitle "HOV Services" --title "Error" --yesno "Sorry user $user exits in system!\nDo you want to try again?" 7 60
    if [ $? -eq 0 ]; then
          rm -f $UP $UN
          getUser
	  getPassword
	  addUsertoSystem "$(cat $UN)" "$UP"
    else
      echo "Bye!"
      exit 1
    fi	  
fi


# add user
echo Adding user $user with password $(cat $2)"
useradd $1
passwd --stdin $user < $2
}

######### main logic ################

getUser
getPassword

addUsertoSystem "$(cat $UN)" "$UP"

rm -f $UP $UN
Hope this helps
Reply With Quote