1

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
Tan
  • 195
  • 1
  • 1
  • 7
  • 2
    Please provide an example output of `dscl localhost -list /LDAPv3`. – Gene Aug 08 '15 at 01:46
  • 1
    Thanks for pointing this out, Gene. Added sample output as requested. – Tan Aug 08 '15 at 02:01
  • It's not clear what you mean by "the second condition for the `else` statement, but when do you think `i` will be equal to 0? `i` is set to each word returned by `dscl` in sequence, and that output appears to consist only of domain names, never the integer 0. – chepner Aug 08 '15 at 02:11
  • @chepner: Thanks for clarifying. I don't think `i` will never equal to 0 and that's where the problem is? I would need to re-test how arguments to the `dscl` command will be interpreted and parsed. – Tan Aug 08 '15 at 02:23
  • It would be clearer if you stated exactly what you want to do in each of the 4 cases. – chepner Aug 08 '15 at 02:26

1 Answers1

1

This statement:

[[ $i !=  0 ]];

Doesn't check if the variable is empty. It just verifies it doesn't equal the integer 0.

Instead try something like this:

[[ ! -z $i ]]

The -z operator will check if the length of the string is zero. Setting ! -z will check if the length isn't zero.

Gene
  • 311
  • 1
  • 10
  • This makes much more sense now. I've added this to the main code which seemed to help add the new domain name if `dscl` returns nothing: `if check_new return 0; then dsconfigldap -v -a {NOD} fi` – Tan Aug 08 '15 at 18:37