LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-22-2010, 09:29 PM   #1
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Rep: Reputation: 15
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
 
Old 12-23-2010, 03:31 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Depends what sort of checks you want to run... If it's just a regular expression thing, then you can use
Code:
if [[ "$variable" =~ .* ]]
then
  #do stuff
fi
replacing .* with an appropriate regular expression. Otherwise, you could write a script in another language such as python and then call that from the bash script.
Code:
if [[ `python checkPassword.py $variable` ]]
then
  #do stuff
fi
Is that what you were looking for?
 
Old 12-23-2010, 09:15 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
How does this break your existing script? I have sent the error messages to stderr instead of stdout as is usefully conventional and moved the exit message to just before the exit (else it lies and that can be confusing):
Code:
/opt/IDEALX/sbin/smbldap-passwd $USERNAME
if [ $? -ne 0 ] ; then
    echo "Removing $USERNAME" >&2
    /opt/IDEALX/sbin/smbldap-userdel -r $USERNAME
    echo 'Exiting on error' >&2
    exit 1
fi
It is easier to read code in CODE tags which can be used most easily in Advanced posting mode via the # button.

Last edited by catkin; 12-23-2010 at 09:16 PM. Reason: Added double quotes for consistency
 
Old 12-24-2010, 09:25 AM   #4
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Snark1994 View Post
Depends what sort of checks you want to run... If it's just a regular expression thing, then you can use
Code:
if [[ "$variable" =~ .* ]]
then
  #do stuff
fi
replacing .* with an appropriate regular expression. Otherwise, you could write a script in another language such as python and then call that from the bash script.
Code:
if [[ `python checkPassword.py $variable` ]]
then
  #do stuff
fi
Is that what you were looking for?

Thanks for the suggestion but I have the code I want to use already. The problem is once I introduce the new if statement it breaks the existing if statement and the script doesn't know what to do if I choose EXAMPLE1 or example2 instead of Default. I guess I need to know how to restructure the script.

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

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

fi

exit
else
echo You entered an invalid Organizational Unit!!
exit
fi
 
Old 12-24-2010, 09:51 AM   #5
woodson2
Member
 
Registered: Oct 2008
Posts: 51

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by catkin View Post
How does this break your existing script? I have sent the error messages to stderr instead of stdout as is usefully conventional and moved the exit message to just before the exit (else it lies and that can be confusing):
Code:
/opt/IDEALX/sbin/smbldap-passwd $USERNAME
if [ $? -ne 0 ] ; then
    echo "Removing $USERNAME" >&2
    /opt/IDEALX/sbin/smbldap-userdel -r $USERNAME
    echo 'Exiting on error' >&2
    exit 1
fi
It is easier to read code in CODE tags which can be used most easily in Advanced posting mode via the # button.
If I change my script to add the checks in bold everything works if you choose Default for the organizational unit, but if you choose EXAMPLE1 or example2 I get errors:

Which LDAP organizational container do you want to add the user to?
+ sleep 1
+ echo '(EXAMPLE1,example2,Default)'
(EXAMPLE1,example2,Default)
+ sleep 1
+ echo If you are unsure please enter Default for the LDAP organizational container.
If you are unsure please enter Default for the LDAP organizational container.
+ read organization
EXAMPLE1
+ [[ EXAMPLE1 = \E\X\A\M\P\L\E\1 ]]
+ echo Please assign a role to this account.
Please assign a role to this account.
+ /opt/IDEALX/sbin/smbldap-passwd tipp
/opt/IDEALX/sbin/smbldap-passwd: user tipp doesn't exist
+ '[' 10 -ne 0 ']'
+ echo 'exiting!!!!'
exiting!!!!
+ /opt/IDEALX/sbin/smbldap-userdel -r tipp
/opt/IDEALX/sbin/smbldap-userdel: user tipp does not exist
+ exit




New 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
fi

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


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

exit

else
echo Sucessfully added $USERNAME to the database!
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

Last edited by woodson2; 12-24-2010 at 09:56 AM.
 
Old 12-24-2010, 10:09 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It would be a lot easier to see what you are doing if you used code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags).

Here's your original script in code tags with some of the blank lines and all of the sleeps removed to make it easier to see the structure.
Code:
#!/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!
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

echo "(EXAMPLE1,example2,Default)"
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
    echo Setting the inital LDAP password for $USERNAME.
    /opt/IDEALX/sbin/smbldap-passwd $USERNAME
    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

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
    echo Setting the inital LDAP password for $USERNAME.
    /opt/IDEALX/sbin/smbldap-passwd $USERNAME
    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
Bash accepts if-fi commands within if-fi commands; what you are trying to do is possible. It might help if you showed us the error message, as requested. It might also help if you could copy the script to a new file and cut it down as much as possible while still producing the error.

Last edited by catkin; 12-24-2010 at 10:10 AM. Reason: groups -> commands
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration