Nikita Spitsky
Nikita Spitsky

Reputation: 79

how to fix Word Splitting

i have bash-script:

for i i in `ldapsearch -xZZLLLD cn=admin,dc=example,dc=com -w -b dc=example,dc=com '(&(objectclass=inetOrgPerson)(memberOf=cn='$groups',ou=Groups,dc=example,dc=com))' dn`
do
echo $i
done

Output:

dn:
uid=user1,ou=People,dc=example,dc=com
dn:
uid=user2,ou=People,dc=example,dc=com

The trouble is that output incorrectly. I read about "word splitting", but don't understand how to fix it. I need get:

dn: uid=user1,ou=People,dc=example,dc=com
dn: uid=user2,ou=People,dc=example,dc=com

Output command:

ldapsearch -xZZLLLD cn=admin,dc=example,dc=com -w -b dc=example,dc=com '(&(objectclass=inetOrgPerson)(memberOf=cn='$groups',ou=Groups,dc=example,dc=com))' dn

dn: uid=user,ou=People,dc=example,dc=com

dn: uid=user1,ou=People,dc=example,dc=com

dn: uid=user2,ou=People,dc=example,dc=com

Upvotes: 0

Views: 47

Answers (1)

heemayl
heemayl

Reputation: 42097

The problem is the output from command has space in each line i.e. it outputs dn: uid=user,ou=People,dc=example,dc=com (with space between dn and uid=user,ou=People,dc=example,dc=com), while iterating over the output with for, because of word splitting on values of IFS (space, tab, newline), you would get two string for each line.

To iterate over each line, use a while-read construct:

ldapsearch -xZZLLLD ... | while IFS= read -r line; do echo "$line"; done

Upvotes: 1

Related Questions