13.07.2015 Views

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

Linux System Administration Recipes A Problem-Solution Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 8 ■ USING THE COMMAND LINE BETTERYou can also write your own completion function. Here’s a very basic function you can add to/etc/bash_completion that looks up a partial hostname in LDAP:01 _ldapcomplete() {02 COMPREPLY=()03 cur=${COMP_WORDS[COMP_CWORD]}04 output=`ldapsearch -Q "(&(cn=$cur*) (objectClass=ipHost))" cn | grep ^cn:`05 name=${output#* }06 COMPREPLY=( ${COMPREPLY[@]} $name )07 return 008 }0910 complete -F _ldapcomplete sshLine 02 sets up the array variable that will be used for the completion return (COMPREPLY). Line 03sets $cur to the value that’s been passed into the function, that is, what’s already been input on thecommand line. The COMP_WORDS variable is set up earlier in /etc/bash_completion for use by all functions.The ldapsearch at line 04 looks for an entry in the LDAP directory, which is a host-type object with aname that begins with the existing input, and then it returns only the cn value of any suitable LDAP entryit finds. The output is piped into grep to strip out all the extraneous LDAP information (the -Q switch isused on the ldapsearch to make the output as quiet as possible). $output will be as follows:cn: myhostSo, line 05 splits on whitespace and takes the second half of $output to give just myhost. This value isput into COMPREPLY at line 06, and the function finishes. In this case, you’ll get only one value, so the linecould be this:COMPREPLY=$nameBut to match the rest of the file and to allow for further improvement, you treat it as an array.■ Note This function does not deal properly with multiple return possibilities (it autocompletes only when LDAPreturns a single value). To deal properly with multiple returns, COMPREPLY would have to be treated as an array.Line 10 allows you to actually use the function. It tells the shell that when using ssh, the_ldapcomplete function should be used for hostname completion.■ Note The documentation on the bash man page is pretty good on completion if you want to investigate further.178Download at WoweBook.Com

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!