![](https://seccdn.libravatar.org/avatar/e38b9efc16cc3779f80fe2b44bf25433.jpg?s=120&d=mm&r=g)
Randall R Schulz wrote:
Jason,
On Friday 19 November 2004 11:54, Jason Joines wrote:
I'm trying to do an ldapsearch in a bash script. I need to be able to expand a variable and use a NOT (!).
If I use double quotes around the filter like this: ldapsearch "(&(cn=$LOGIN)(!(usertype=student)))" bash complaines about "bash: !: event not found" and doesn't even execute the command.
If I use single quotes around the filter like this: ldapsearch '(&(cn=$LOGIN)(!(usertype=student)))' the command runs but the variable does not get expaned.
If I use a backslash escape like this: ldapsearch "(&(cn=$LOGIN(\!(usertype=student)))" ldapsearch complains about "ldapsearch: ldap_search_ext: Bad search filter (-7)"
Any ideas?
I'm completely unfamiliar with LDAP and related tools, but I can tell you about BASH quoting and escaping:
- You can mix and match quoting styles. If parts of the argument are laden with character special within double quotes ($ or !, e.g.), enclose those parts within single quotes (a.k.a. apostrophes) and switch to double quotes when you need variable interpolation or history expansion. In order to produce a single argument, leave no space between the two (or more) differently quoted segments.
- Replace your command with echo to see what's being passed:
% echo "(&(cn=$LOGIN(\!(usertype=student)))" (&(cn=(\!(usertype=student)))
So, now you can see that the escape that prevented history expansion got passed on to ldapsearch, too.
Presumably this command will do what you want:
% ldapsearch "(&(cn=$LOGIN)"'(!(usertype=student)))'
Jason Joines
Randall Schulz
Thanks for the tip. I didn't even theink to try echoing it. I did try "(&(cn=$LOGIN)'(!(usertype=student))')". When I echoed it as you suggested I saw that the single quotes go preserver which is what made ldapsearch mad. I wouldn't've though to just append them as you suggested but "(&(cn=$LOGIN)"'(!(usertype=student)))' did the trick. Thanks again, Jason ===========