通过 BASH 脚本,我会自动构建一个字符串,例如:
DC=us,DC=earth,DC=com
for LDAP 域组件。但是,我不知道一个域可能有多少个子域组件。
例如:
dub.sub.domain.tld
需要
DC=dub,DC=sub,DC=domain,DC=tld
sub.domain.tld
需要
DC=sub,DC=domain,DC=tld
domain.tld
需要
DC=domain,DC=tld
我目前的解决方案是:
# The variable we got somehow
indomain="dub.sub.domain.tld"
#IFS='.' #Please no, sed instead...
# Create a loopable list with space as the IFS
domaincomponents=$(echo $indomain | sed 's/\./ /g')
# Process each domain component
finalstring=""
for component in $domaincomponents; do
finalstring=DC=$component,$finalstring
done
# Remove the trailing ,
finalstring=$(echo $finalstring | sed 's/\(.*\),/\1 /')
结果:$finalstring
=
DC=dub,DC=sub,DC=domain,DC=tld
awk
这样的工具可以做得更干净。有更好的方法还是这是最好的?
我假设LDAP中的DC=
可以深入到任何子域名;如果我错了请纠正我。
sed
:
echo 'dub.sub.domain.tld' | sed 's/^/DC=/; s/\./,DC=/g'
输出:DC=dub、DC=sub、DC=域、DC=tld