If your code's as you post it, it has to have occurred somewhere, but if it's not, other problems are possible.
Could you have done '$NAME' instead of "$NAME" ? That definitely will not work, and eval definitely would have "fixed" it. Using double quotes is the better answer of course.
What is the -d '\0' for?
That's a no-op unless your input actually has nulls, and yours doesn't.
That's a consideration when using read to dump large lists of things into arrays and variables. But you may not need an array at all.
make_dir () {
for i in "/var/lib/ldap" "/var/lib/ldap/accesslog" "/var/tmp/bdb-log" "/etc/openldap/slapd.d" ; do
if [ -d "$i" ] ; then
:
else
mkdir "$i"
fi
done
if [ $? -eq 0 ] ; then
printf "%b\n" "Checking directories: [ \e[92mOK\e[0m ]"
else
printf "%b\n" "Missing directories, unable to create them: [ \e[91mFAILED\e[0m ]"
fi
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
}
That if [ $? ]
doesn't do what you think it does. It's only checking the very last loop, not any of the ones before. You can replace the whole thing with one mkdir call, anyway:
mkdir has -p which will replace most of your function:
make_dir () {
if ! mkdir -p "/var/lib/ldap" "/var/lib/ldap/accesslog" "/var/tmp/bdb-log" "/etc/openldap/slapd.d"
then
printf "%b\n" "Missing directories, unable to create them: [ \e[91mFAILED\e[0m ]"
return
fi
printf "%b\n" "Checking directories: [ \e[92mOK\e[0m ]"
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
}
set_permissions () {
for i in "/var/lib/ldap" "/var/tmp/bdb-log" "/etc/openldap/slapd.d" ; do
chown -R ldap:ldap "$i"
done
if [ $? -eq 0 ] ; then
printf "%b\n" "Setting permissions: [ \e[92mOK\e[0m ]"
return 0
else
printf "%b\n" "Unable to set permissions: [ \e[91mFAILED\e[0m ]"
return 1
fi
}
Same problem here, your $? doesn't do what you think it does, you could have used if directly, and chown can accept multiple names anyway.
If your ldap permissions are changing all the time, it might be a good idea to find what's doing so...
set_permissions () {
if chown -R ldap:ldap "/var/lib/ldap" "/var/tmp/bdb-log" "/etc/openldap/slapd.d"
then
printf "%b\n" "Setting permissions: [ \e[92mOK\e[0m ]"
return 0
else
printf "%b\n" "Unable to set permissions: [ \e[91mFAILED\e[0m ]"
return 1
fi
}
Never, ever use kill -9 as anything but a last resort. You have to ask it to stop, wait, kill it politely, wait, kill it less politely, wait, and if it doesn't die, THEN you can murder it:
stop_ldap () {
pid_number=$(pgrep slapd)
systemctl stop slapd
sleep 2
for SIG in TERM INT HUP KILL # Increasingly severe signals to kill it with
do
pgrep slapd >/dev/null || break
kill -$SIG "$pid_number"
sleep 1
done
}
I'll have to think about the rest.