I am scripting up an app that will help users bind to our server's open directory if one does not exist. It will test if the command's output is empty or not and help remove any old open directory and add the new one.
I am running into issues with how I should go about adding a new domain name to the Open Directory Server if the script experiences certain conditions. What am I doing wrong? See output for examples...
#!/bin/bash -x
# FUNCTIONS
# Verify that foo.com exists
function check_new () {
# LOCAL VARIABLES
OLD="bar.com"
NEW="foo.com"
for i in `dscl localhost -list /LDAPv3`; do
if [[ $i != 0 ]]; then
dsconfigldap -v -r ${OLD} && dsconfigldap -v -a ${NEW}
else
dsconfigldap -v -a ${NEW}
fi
done
}
# MAIN CODE
exec 1> >(logger -s -t $(basename $0)) 2>&1
check_new; exit 0
SAMPLE OUTPUT
Case 1) No servers exist (returns nothing if we don't expand the $?
exit status. We should add the new domain name.
$ dscl localhost -list /LDAPv3; echo $?
0
Case 2) Old server exist. We should remove the old server and add the new one.
$ dscl localhost -list /LDAPv3; echo $?
bar.com
0
Case 3) Old AND New servers exist. Ignore the new server and remove the old one.
$ dscl localhost -list /LDAPv3; echo $?
foo.com
bar.com
0
Case 4) NEW server exist. Do nothing.
$ dscl localhost -list /LDAPv3; echo $?
foo.com
0
This statement:
Doesn't check if the variable is empty. It just verifies it doesn't equal the integer 0.
Instead try something like this:
The
-z
operator will check if the length of the string is zero. Setting! -z
will check if the length isn't zero.