0

I'm trying to execute a ldapsearch command inside a script.

Following does not work

ADMIN_USER="$(whoami)";

ldapmodify -h myldapserver  -p 23223 -D 'uid=$ADMIN_USER,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;

The above code gets executed with following error ldap_bind: Invalid credentials (49)

If hardcode the value like below, then it works.

ADMIN_USER="$(whoami)";

ldapmodify -h myldapserver -p 23223 -D 'uid=adminuser,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;

Is there a sepcific reason for this? how will i be able to fix this?

Chamara Keragala
  • 5,627
  • 10
  • 40
  • 58

1 Answers1

3

Bash isn't expanding the variable because you enclosed it in single quotes. Change the string to use double quotes:

ADMIN_USER="$(whoami)";
ldapmodify -h myldapserver -p 23223 -D "uid=${ADMIN_USER},ou=people,ou=company,dc=corpcom,dc=com" -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;

See also this answer.

Community
  • 1
  • 1
Julian
  • 2,837
  • 17
  • 15
  • This skips having the single-quote around the arguments to `-D` just expands the variable alone – Inian Jan 10 '17 at 12:51
  • That's right, it's my assumption that is desired. – Julian Jan 10 '17 at 12:52
  • @Inian The single quotes in the original aren't passed to the command anyway; they are removed by the shell prior to calling the command. They are only used to escape each character contained in them. – chepner Jan 10 '17 at 13:43
  • @chepner : thought it is needed as part of the flag – Inian Jan 10 '17 at 14:02
  • @chepner : Also the syntax I used actually expanded the variable properly. – Inian Jan 10 '17 at 14:04
  • The expansion is not the problem; the *interpretation* of the expanded value by the shell is. – chepner Jan 10 '17 at 14:38