Register
It is currently Thu Dec 18, 2014 11:36 am

Help with bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Dec 22, 2010 9:53 pm   

Joined: Mon Apr 27, 2009 2:24 pm
Posts: 10
My script is below and it all works well and fine, however I need to have checks put in for erroneous user input during the smbldap-passwd functions and the ldapadd functions.

I've figured out a statement that will look at the exit status of the command and it will remove the user that was added in the previous step and exit but I can't figure out how to put these if statements into my existing script without breaking it. Yes, I'm a new shell scripter so any help will be greatly appreciated. Thanks

/opt/IDEALX/sbin/smbldap-passwd $USERNAME
if [ $? -ne 0 ] ; then
echo exiting!!!!
/opt/IDEALX/sbin/smbldap-userdel -r $USERNAME




Exisiting script:

#!/bin/bash -x
TMPFILE=/db/backups/tmp-expire.ldif
TMPFILE2=/db/backups/tmp-expire-ou.ldif
TMPFILE3=/db/backups/variable3-ou.ldif
TMPFILE4=/db/backups/variable4-ou.ldif

echo Please enter the username you would like to add to LDAP!

read USERNAME

if getent passwd | grep -wq $USERNAME

then
echo $USERNAME already exists in the LDAP database!
exit
fi

echo Please enter the menu group to associate with this account!
sleep 1

echo "(guser1,guser2,gsuer3,guser4,guser5,guser6,guser7,guser8,guser9,gadmin,gsuper)"

read GUSER

if [[ "$GUSER" = guser* || "$GUSER" = "gadmin" || "$GUSER" = "gsuper" ]]; then

echo Which LDAP organizational container do you want to add the user to?
else
echo You entered an invalid group!!
exit
fi

sleep 1

echo "(EXAMPLE1,example2,Default)"
sleep 1
echo If you are unsure please enter Default for the LDAP organizational container.

read organization

if [[ "$organization" = "EXAMPLE1" || "$organization" = "example2" ]]; then

echo Please assign a role to this account.

elif [ "$organization" = "Default" ]; then
/opt/IDEALX/sbin/smbldap-useradd -G 1513,$GUSER,26 -s /bin/ksh -d /home/operations/$USERNAME -a $USERNAME
sleep 1

echo Setting the inital LDAP password for $USERNAME.
sleep 1

/opt/IDEALX/sbin/smbldap-passwd $USERNAME
sleep 1

echo Enforcing password expiration upon first login!!!!!!

cat $TMPFILE | sed "s/USER/$USERNAME/g" /db/backups/tmp-expire.ldif > /db/backups/variable3.ldif
ldapadd -f /db/backups/variable3.ldif -x -D cn=root,dc=mdvcat,dc=lott -W

exit
else
echo You entered an invalid Organizational Unit!!
exit
fi

sleep 1
echo "(admins,network,developers,vendors)"

read role

if [[ "$role" = "admins" || "$role" = "network" || "$role" = "developers" || "$role" = "vendors" ]]; then


/opt/IDEALX/sbin/smbldap-useradd -G 1513,$GUSER,26 -o $role,$organization -s /bin/ksh -d /home/operations/$USERNAME -a $USERNAME

sleep 1

echo Setting the inital LDAP password for $USERNAME.

sleep 1

/opt/IDEALX/sbin/smbldap-passwd $USERNAME

sleep 1

echo Enforcing password expiration upon first login!!!!!!

cat $TMPFILE2 | sed -e "s/USER/$USERNAME/g" /db/backups/tmp-expire-ou.ldif > /db/backups/variable3-ou.ldif
cat $TMPFILE3 | sed "s/role/$role/g" /db/backups/variable3-ou.ldif > /db/backups/variable4-ou.ldif
cat $TMPFILE4 | sed "s/organization/$organization/g" /db/backups/variable4-ou.ldif > /db/backups/variable5-ou.ldif

ldapadd -f /db/backups/variable5-ou.ldif -x -D cn=root,dc=mdvcat,dc=lott -W


else
echo You entered an invalid role!!!
exit
fi


Top
 Profile  
 PostPosted: Thu Dec 23, 2010 10:19 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
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


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP