I am configuring an LDAP server from a bash script and I need to convert the fully qualified domain name (FQDN) to ldap DNs.
For example:
My bash function works, but it is a little bit verbose. Is there any built-in one-line bash command that I can use? Or how to make my function less verbose?
My code:
#!/bin/bash
function fqdn_to_ldap_dn() {
local fqdn parts dn
fqdn="$1"
IFS=. parts=(${fqdn##*-})
dn=""
for i in "${parts[@]}" ; do
dn+="dc=$i,"
done
dn=${dn%?};
echo $dn
}
echo $(fqdn_to_ldap_dn "aa.hello.com")
Something like this will work:
fqdn_to_ldap_dn() {
sed -e 's/[^ ]*/dc=&/g' <<<"${1//./ }" -e 's/ /,/g'
}
Wrapping that in the following script:
#!/bin/bash
fqdn_to_ldap_dn() {
sed -e 's/[^ ]*/dc=&/g' <<<"${1//./ }" -e 's/ /,/g'
}
domains="
com
world.com
hello.world.com
hello.beautiful.world.com
"
for domain in $domains; do
echo "$domain -> $(fqdn_to_ldap_dn $domain)"
done
We get:
com -> dc=com
world.com -> dc=world,dc=com
hello.world.com -> dc=hello,dc=world,dc=com
hello.beautiful.world.com -> dc=hello,dc=beautiful,dc=world,dc=com
Breaking down that command a bit, we have:
${1//./ /}
replace all instances of .
in $1
with
(a space). See the "Parameter Expansion" section of the Bash man page for details.
The sed
expression s/[^ ]*/dc=&/g
searches for all groups of non-space characters and adds dc=
in front of them (the &
in the replacement means "whatever we matched in the first part of the expression").
The sed
expression s/ /,/g
replaces all spaces with ,