ldapsearch does not check for the correct return value when its output is redirected
to a file system that has no space left. The $? value shows up as 0 even though
the output has not been written to the file.
Work Around
Pipe the output of ldapsearch with a command that correcly handle error printing to stdout, such as cat(1). Using the example given in comments, that would give something like:
# ldapsearch -h 10.5.60.36 -p 389 -b dc=example,dc=com "objectclass=*" dn | cat > ldap_output.txt
cat: output error (0/6731 characters written)
No space left on device
# echo $?
2
#