while creating a script I am facing currently a "strange" issue that so far I am unable to fix.
typeset -r SERVICE=\"ldap://localhost:10389\ ldaps://solsrv02.internal.vbox:10689\ ldaps://solsrv02.prod.internal.vbox:10689\"
SLAPD="/usr/lib/slapd -u ${LDAPUSR} -g ${LDAPGRP} -h ${SERVICE} -F ${CONF_DIR}"
Currently the variable assignment is like above and when I echo the SLAPD it works like a charm. but when I execute the command : Echo output
/usr/lib/slapd -u openldap -g openldap -h "ldap://localhost:10389 ldaps://solsrv02.internal.vbox:10689 ldaps://solsrv02.prod.internal.vbox:10689" -F /etc/openldap/standalone
Debug output
+ [[ ! -d /var/openldap/run ]]
+ /bin/rm -f /var/openldap/run/slapd.pid
+ /usr/lib/slapd -u openldap -g openldap -h '"ldap://localhost:10389' ldaps://solsrv02.internal.vbox:10689 'ldaps://solsrv02.prod.internal.vbox:10689"' -F /etc/openldap/standalone
So, my issue now, is that openldap command line forces me to have -h "URLs" and adding then to the variable it takes the variable as 3 parameters instead of a single as I wish.
I have tried to backspace the quotes but it expands to 3 single quotes. If I just copy the output of echo and paste it on the script it works. but this has the downside that I cannot customize using variables.
all the variables have been set with typeset -r
UPDATE 1
Based on the given answer by cas, I updated my code but still facing the same issue:
#!/usr/bin/bash
echo $SHELL
typeset -r SLAPD=/usr/lib/slapd
typeset -r LDAPUSR=openldap
typeset -r LDAPGRP=openldap
typeset -r VARRUNDIR=/var/openldap/run
typeset -r PIDFILE=${VARRUNDIR}/slapd.pid
typeset -r CONF_DIR=/etc/openldap/standalone
typeset -r SERVICE=(ldap://localhost:10389 ldaps://solsrv02.internal.vbox:10689 ldaps://solsrv02.prod.internal.vbox:10689)
SLAPARGS=" -u ${LDAPUSR}"
SLAPARGS+=" -g ${LDAPGRP}"
SLAPARGS+=" -F ${CONF_DIR}"
SLAPARGS+=" -h "
SIZE=${#SERVICE[@]}
#for i in {1..${#SERVICE[@]}$}; do
SLAPARGS+="'"
for ((i=0;i<$SIZE;i=$i+1)); do
SLAPARGS+="${SERVICE[$i]} "
done
SLAPARGS+="'"
[[ ! -d ${CONF_DIR} ]] && exit $SMF_EXIT_ERR_CONFIG
echo "$SLAPD $SLAPARGS"
set -x
$SLAPD $SLAPARGS 2>&1
the output is this:
/usr/bin/bash
/usr/lib/slapd -u openldap -g openldap -F /etc/openldap/standalone -h 'ldap://localhost:10389 ldaps://solsrv02.internal.vbox:10689 ldaps://solsrv02.prod.internal.vbox:10689 '
+ [[ ! -d /var/openldap/run ]]
+ /bin/rm -f /var/openldap/run/slapd.pid
+ /usr/lib/slapd -u openldap -g openldap -F /etc/openldap/standalone -h ''\''ldap://localhost:10389' ldaps://solsrv02.internal.vbox:10689 ldaps://solsrv02.prod.internal.vbox:10689 ''\'''
Bash version is 4.1.17
SERVICE
to be read-only? 2. don't use\"
when definingSERVICE
, use single-quotes instead. 3. use double-quotes around$SERVICE
when you use it on the slapd command line.SLAPARGS+="$SLAPARGS -h '$SERVICE[@]}'"
- rememberslapd
's-h
option requires a single string as an arg. BTW,set -x
will try to print quoted strings in a format that can be copy-pasted, which may not be exactly the same as what was executed....and the correct way to embed a single-quote inside single quotes is'\''
– cas Apr 04 '16 at 13:11rsync
backup scripts and mydebmirror
wrapper script, and in many other scripts. the method works exactly as i want/expect it to in all those scripts. – cas Apr 04 '16 at 13:16