I have a script to get ldap user name, email and mobile number:
#!/bin/bash
echo -n "Enter Unix id > "
read UNIXID
ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/'
Here is the output of the script:
#./lsearch
Enter Unix id > in15004
givenName: Mr. Xyz
mail: [email protected]
mobile: 9xxxxxxxx1
Now I want to modify the script so that I can run it in non-interactive mode, e.g:
#./lsearch –i in15004 # (i meand ID)
givenName: Mr. Xyz
mail: [email protected]
mobile: 9xxxxxxxx1
or:
#./lsearch –n Xyz* # (n means givenName)
givenName: Mr. Xyz
mail: [email protected]
mobile: 9xxxxxxxx1
or:
./lsearch –e x@*.com # (e means email id)
givenName: Mr. Xyz
mail: [email protected]
mobile: 9xxxxxxxx1
How can I do that? I tried below :
#!/bin/bash
while getopts "i:" OPTION; do
case $OPTION in
i)
UNIXID=$OPTARG
;;
esac
done
ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/'
#ldapsearch -x "(mail=$MAIL)" | awk '/givenName/||/mobile/||/mail/
#ldapsearch -x "(givenName=$NAME)" | awk '/givenName/||/mobile/||/mail/
exit 0;
Here is the output of the script:
#./lsearch -i in15004
givenName: Mr. Xyz
mail: [email protected]
mobile: 9xxxxxxxx1
I think similar like above will do. But not sure how to make the loop.
ldapsearchcommand with the field names that you want, it will returndnplus only those selected fields.