Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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.

share|improve this question
    
what did you already try doing? –  Eric Renouf Apr 17 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 at 13:23

3 Answers 3

up vote 2 down vote accepted

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.

share|improve this answer
1  
FWIW, when using getopts its easy to support a -h option to print a help message for the user. –  PM 2Ring Apr 17 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 [email protected] is not working. –  SM_IND Apr 20 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 at 12:37

Instead of getopts you can also simply use the built-in Bash variables $1, $2 etc. inside your script. These variables are automatically assigned to the first, second etc. argument passed to the script.

So if your program is run with ./lsearch –i in15004, inside the script $1 will take the value -i and $2 the value in15004.

The name of the script, in this case ./lsearch, is assigned to the variable $0.

Then you can pass these variables to the ldapsearch command in the appropriate way.

share|improve this answer
1  
getopt (and the builtin getopts) works very well inside shell scripts I've been writing for quite a number of years. –  roaima Apr 17 at 13:20
    
Thanks for the correction. I've edited my post. –  dr01 Apr 17 at 13:26
    
Built in variables was my first thought too, but he needs to pass the flag so he can modify the search based on one argument which could apply for a variety of ldap fields. –  Baazigar Apr 17 at 14:04
    
This can be done with built-in Bash variables too. –  dr01 Apr 17 at 15:32

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: [email protected]

mobile: 9xxxxxxxx1
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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