Hi there Woodson2!
There are a few ways you could probably do this. The simplest way is to use exactly what you have written there!
The variable "$?" is the return code of the most recent command, so you should be able to put that in-line with your existing script with no problems. Something like this should work:
Code:
echo Setting the inital LDAP password for $USERNAME.
sleep 1
/opt/IDEALX/sbin/smbldap-passwd $USERNAME
if [ $? -ne 0 ] ; then
echo 'something went horribly wrong... reversing changes and exiting!'
/opt/IDEALX/sbin/smbldap-userdel -r $USERNAME
exit 1
fi
sleep 1
echo Enforcing password expiration upon first login!!!!!!
Another thing that might help is breaking up chunks of this script into functions. That way you can evaluate the user input, and loop back to the beginning of the function if you need to. Here's an example:
Code:
function get_user() {
echo 'Please enter the username you would like to add to LDAP!'
read -p "Username [or Q to quit]: " USERNAME
if [ "$USERNAME" = "Q" ]; then
echo 'Thank you, come again!'
exit
fi
if getent passwd | grep -wq $USERNAME; then
echo "$USERNAME already exists in the LDAP database"
echo 'Try again!'
get_user
fi
}
Anytime you call 'get_user' in your script, it will run this block of code. If the username exists, the function calls itself and prompts for a username again!
I hope this helps!
-Jeo