Here's my script. Wanted to make it good. Got some great practice with various loops and functions as well as putting in failsafes and making it a bit interactive 
Code:
#!/bin/bash
#variables to be used...
newuser= #variable used to create a new user
currentuser=$HOME #current user variable, reads $HOME variable
yesno= #variable for yes/no answers
rootid=0 #vairable to determine root status
newexist= #used to determine if we are using a new or existing user
#newuser function
newuser()
{
echo "what would you like the new user's name to be"
read newuser
echo "the new user will be $newuser"
echo "creating new user..."
adduser $newuser
}
#function to ask if the user is new or old
newold()
{
echo "Are you creating a new user or moving files to an existing account"
echo '[new/existing]'
read newexist
if [ "$newexist" = "new" ]; then
newuser
else
echo "what is the name of the account you wish to move files to"
read newuser
fi
}
#this function copies all the user's files over
moveuser()
{
echo "the current user is $currentuser"
echo "do you wish to move this user to /home/$newuser ?"
echo "y/n"
read yesno #asks for user input
if [ "$yesno" = "n" ] || [ "$yesno" != "y" ] #if the answer is n or not y then ask for new user name
then
echo "enter the username of the account to be moved"
read currentuser #gets user input and puts into currentuser variable
echo "$currentuser"
currentuser=/home/$currentuser #if $currentuser is left as just an inputed name "user" then it will try to use user instead of /home/user so set variable to use /home/$currentuser
echo "$currentuser"
echo "this is the right user?"
echo "[y/n]"
read yesno
if [ "$yesno" != "y" ]; then #this if statement is needed to prevent wrong inputs, eg no instead of n
echo "you have not entered a valid answer"
echo "exiting status 1"
exit 1
fi
fi
#when the user is set right it now moves all the files over to the new user
mv "-f" $currentuser/* /home/$newuser #moves files recursively and forcefully, probably need if we use the adduser method
echo "files have been moved from $currentuser to $newuser"
echo "removing old files"
rm -Rf $currentuser/*
cd $currentuser #moves to the old user's folder
echo "making new directories"
mkdir Desktop Documents Downloads Music Pictures Videos #makes all the default user directories for ubuntu
echo "changing user permissions"
chown $newuser /home/$newuser/* #changes file ownership, otherwise everything is owned by root
chmod 755 /home/$newuser/* #changes file permissions, rwx,r-x,r-x, read,write, execute for user and read execute for else
}
#the script
#start off testing for root, because this cannot be done without it and should exit if you are not root
if [ "$UID" -eq "$rootid" ] #checks for root
then
newold #calls newuser function
moveuser #calls move user function
else #If you are not root then it exits
echo "You are not root. Run the script again with sudo"
echo "exiting now"
exit 0
fi
exit 0
Bookmarks