2

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: x@abc.com
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: x@abc.com
mobile: 9xxxxxxxx1

or:

#./lsearch –n Xyz* # (n means givenName)
givenName: Mr. Xyz
mail: x@abc.com
mobile: 9xxxxxxxx1

or:

./lsearch –e x@*.com # (e means email id)
givenName: Mr. Xyz
mail: x@abc.com
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: x@abc.com
mobile: 9xxxxxxxx1

I think similar like above will do. But not sure how to make the loop.

  • what did you already try doing? – Eric Renouf Apr 17 '15 at 12:23
  • If you suffix your ldapsearch command with the field names that you want, it will return dn plus only those selected fields. – roaima Apr 17 '15 at 13:23
2

If you want to use getopts (noted the "s") to get the command line arguments you can do something like

while getopts "i:n:e:" OPT; do
    case "$OPT" in
        i)
            # do stuff with the i option
            ID="$OPTARG"
            ;;
        n)
            # do stuff with the n option
            ;;
        e)
            # do stuff with the e option
            ;;
    esac
done

The getopts takes 2 arguments, a string saying what options it should look for and the name of the variable to store the current option it found in. The string to tell it what options to look for is the letter for the short option, and if that later is followed by a colon it means the option takes an argument, it isn't just a flag that is set.

  • 1
    FWIW, when using getopts its easy to support a -h option to print a help message for the user. – PM 2Ring Apr 17 '15 at 13:25
  • @Eric.Renouf Got it, but how to do the looping? #./lsearch -i in15004 is working but #./lsearch -n Mr. Xyz or #./lsearch -m x@abc.com is not working. – SM_IND Apr 20 '15 at 9:15
  • @SM_IND I'm not sure what loop it is that you're talking about trying to include there. Looking at the sample code you posted, you are only processing one flag, -i. If you want to handle the others you'll need to add those to the string you pass to getopts and then add a case for each flag. You can have the case actually call your lsearch without just setting variables if that helps too – Eric Renouf Apr 20 '15 at 12:37
0

This is quite basic. You can pass the name as an argument.

#!/bin/bash

UNIXID=$1

ldapsearch -x "(cn=$UNIXID)" | awk '/givenName/||/mobile/||/mail/'

#####################


#./lsearch in15004

givenName: Mr. Xyz

mail: x@abc.com

mobile: 9xxxxxxxx1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.