I have the following bash script for some reason $LINE value is not coming on val=. I can see the line from file is read can be display by each $LINE but when defined in the val.. is not showing.

  while read LINE
       echo "Processing:" $LINE
       val=`ldapsearch -h localhost -w test -z 0 -x -b 'dc=test,dc=org' -D "cn=admin,dc=testing,dc=com" id=$LINE | grep val`
       echo $LINE "," $val
  done < inputfile.txt

any idea what I am doing wrong here.

  • 1
    Take one example line. Replace $LINE with the (quoted) text. Then run the ldapsearch command. Does that give the intended result? – Hauke Laging May 3 '14 at 0:45

Based on your issue it would seem like you're encountering an issue with the results of your ldapsearch.

Potential fix #1

For starters I'd enable verbose output so you can see how the backtick portion of your command is getting expanded.

set -x
val=`ldapsearch -h localhost -w test -z 0 -x -b 'dc=test,dc=org' -D \
     "cn=admin,dc=testing,dc=com" id=$LINE | grep val`
set +x
Potential fix #2

I'd also change it so that it uses $() notation instead of the backticks.

val=$(ldapsearch -h localhost -w test -z 0 -x -b 'dc=test,dc=org' -D \
     "cn=admin,dc=testing,dc=com" id=$LINE | grep val)
Potential fix #3

Also I'd quote the contents of $LINE when passed to id=.

val=$(ldapsearch -h localhost -w test -z 0 -x -b 'dc=test,dc=org' -D \
     "cn=admin,dc=testing,dc=com" id="$LINE" | grep val)
Potential fix #4

Lastly I'd try changing the pipe from | to |& so that you're parsing both STDOUT and STDERR from ldapsearch.

General tip

You can always enable full verbose output from Bash by appending the switch -x to the shebang line.

#!/bin/bash -x
  • this is great info. It helps me to narrow down the problem. I noticed there is \r in $LINE which is massing the ldap query. what is the best way to remove this line feed from this variable. I am $LINE | tr "\r" " ". which is kind of working – Raza May 3 '14 at 1:44
  • 1
    @Salton - yes tr would be the way to go. Do a | tr -d '\r'. That'll delete any \r's from your output. – slm May 3 '14 at 1:50

That is probably a quoting problem. Instead of id=$LINE you should (in general) use id="$LINE". Same for echo $LINE.

  • no sir. not that easy.. I tried that didn't work.. it is frustrating. – Raza May 3 '14 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.