krish
krish

Reputation: 1438

ldapsearch output to a text file

I am writing a bash script to return the display names for users. When I run the command on the console, I get the display name. When I run the script, which reads inputs from a text file, I do not get the display name.

For [email protected], the display name is like: Khbsd Muweu

The input text file (names.txt) contains the following data:

"[email protected]"  
"[email protected]"  
"[email protected]"  

The script is as below:

while IFS='' read -r line || [[ -n "$line" ]]
do
echo "$line"
ldapsearch -v -LLL -Y GSSAPI -H ldap://padns1.abc.com -b "dc=abc,dc=com" userPrincipalName="$line" displayName -Q | grep "displayName: .*$" | awk '{print $2,$3}'
done < "$1"

This script is saved as fnames.sh and the script is run as:

bash fnames.sh names.txt

The echo "$line" prints the content from the text file, but the ldapsearch command does not work. Can someone let me where I am wrong? I want to store the output of the command (just the display name) in a text file. How can I do that?

Upvotes: 0

Views: 4138

Answers (1)

Mr. Llama
Mr. Llama

Reputation: 20909

If your input file actually contains those double quotes, that's what's causing the issue.

Once expanded, you'll be using userPrincipalName=""[email protected]"", that is to say, literally looking for "[email protected]" (with quotes), not [email protected] (without quotes). You should also check that your input file is using the correct line endings. Carriage returns mixed in can silently wreak havoc.

Upvotes: 2

Related Questions