Thursday, April 16, 2009

Looping Over an Indefinite Number of Command Line Parameters

It's often the case that you'll have to do something on a big list of MDNs or other parameters. To expedite your processing, you can make a script to lop over any number of parameters and execute a command. This example will run an LDAP querey on every number you add as a parameter:

while [GXMMS: "$1" != "" ]
do
/lsurf/openldap-2.1.17/bin/ldapsearch -x -H ldap://localhost:9009 -b ou=consumer,o=sprintpcs mdn=$1
echo $'\a'
shift
done

so instead of having to paste in each number, I can just enter this command:

BulkLDAPSearch.sh 9178069377 9173098528

The key elements here are the while loop and the shift command, which pops the first argument off ot the parameter list so the next takes its place. The "echo $'\a'" bit makes the computer beep in between each iteration (turn it off before it gets annoying!).

No comments:

Post a Comment