bashldapldap-queryreturn-code

Have LDAP Search Return a 1 if a record is found


Hello again Stackoverflow,

Is there a method to have ldapsearch return a "1" if a single record is found, 0 if no record is found or 2(or larger) if multiple records are found in ldap. I would like to be able to test for the record, and if a 1 is returned, then take some predefined action. I know I can set $something=0 then do a ldapsearch >> $something and then test if $something!=0 but this will not be an efficient use of memory or process time and may lead to unexpected "fun" in my script. This would also require extra tests to be performed against $something that will add more time to the batch job.

ldapsearch -LLL -h ds389.some.org -p 389 -D "uid=someAdmin,o=some.org" -w SOMEpassWord -b "ou=People,o=some.org" -s sub '(&(RMailAliases=zzz)(!(RMid=d*)))'


Solution

  • echo `ldapsearch -LLL -h ds389.some.org -p 389 -D "uid=someAdmin,o=some.org" -w SOMEpassWord -b "ou=People,o=some.org" -s sub '(&(RMailAliases=zzz)(!(RMid=d*)))' dn | grep dn: | wc -l` should do what you want.