Results 1 to 6 of 6

Thread: Help with bash script

  1. #1

    Help with bash script

    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

  2. #2
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    So what are you asking?

    if it is possible to use $? in the script or if theres some other way ?

    if it was me I probably would use:
    Code:
    RET=`/opt/IDEALX/sbin/smbldap-passwd $USERNAME`
    if [ $RET -ne 0 ] ; then 
      blah blah DOH!
    else
      weee
    fi
    Then you are keeping the return value, and are able of reported something like exited with return code blah, or make a suggesten decided from the return code.
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

  3. #3
    OK..Let me explain a bit clearer. If I try to change the code to look like whats in bold it works as long as I choose Default for the organization but then if I choose example1 or example2 then it bombs out.

    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
    if [ $? -ne 0 ] ; then
    echo exiting!!!!
    /opt/IDEALX/sbin/smbldap-userdel -r $USERNAME

    fi

  4. #4
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    hmm never tried anythign with several conditions in a single if() test, I woud probable use a case statemen.

    Code:
    case "$organization" in
      "EXAMPLE1" | "example2" )
         echo Please assign a role to this account.
         ;;
      *) # Default behaviour to catch anything else
        /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
        if [ $? -ne 0 ] ; then
            echo exiting!!!!
            /opt/IDEALX/sbin/smbldap-userdel -r $USERNAME
        fi
        ;;
    esac
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

  5. #5
    Thanks. I'll give that a try and let you know how it goes.

  6. #6
    Just wanted to thank you for this code. I was able to get it working..Much appreciated!!

    Quote Originally Posted by redhead View Post
    hmm never tried anythign with several conditions in a single if() test, I woud probable use a case statemen.

    Code:
    case "$organization" in
      "EXAMPLE1" | "example2" )
         echo Please assign a role to this account.
         ;;
      *) # Default behaviour to catch anything else
        /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
        if [ $? -ne 0 ] ; then
            echo exiting!!!!
            /opt/IDEALX/sbin/smbldap-userdel -r $USERNAME
        fi
        ;;
    esac

Similar Threads

  1. SSH and bash script
    By vwgtiturbo in forum Programming
    Replies: 7
    Last Post: 12-25-2008, 05:40 AM
  2. Bash script problem
    By Schotty in forum Programming
    Replies: 1
    Last Post: 04-20-2007, 01:18 AM
  3. Bash script
    By linuxuser in forum Programming
    Replies: 1
    Last Post: 09-01-2005, 03:21 PM
  4. Help with bash script
    By tolstoy in forum Programming
    Replies: 5
    Last Post: 05-21-2002, 11:17 AM
  5. Bash script for sorting mp3's.
    By airhead in forum Programming
    Replies: 7
    Last Post: 02-24-2002, 07:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •