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 BETTER■ Note This won’t tab-complete. You’ll need to type the full directory name in (research, in this example), unlessyou’re cding to a directory in your current directory, in which case tab-complete functions as normal.However, the major improvement available for the regular autocompletion is programmablecompletion. This more or less borrows from zsh, which has extensive autocompletion and a few otherinteresting options (it should be available as a package for your distro if you want to investigate).You can write completion functions all by yourself, but to avoid extensive wheel reinvention, you’rebetter off downloading /etc/bash_completion from http://www.caliban.org/bash/ (also available as apackage for most distros and installed by default in Ubuntu as the bash-completion package and Debianin the bash package) and then sourcing that file in your ~/.bashrc file:source /etc/bash_completionNow open a new terminal window, and then try typing ssh at the command line and then hittingTab. You’ll be shown a list of possible hosts, based primarily on your known_hosts file (along with acouple of other tricks). Various other commands are also set up to do this by the /etc/bash_completionfile. Experiment with traceroute or ftp.You can also extend this particular form of completion to other commands or to your own scripts.Take a look at the /etc/bash_completion file. It’s pretty big, but you can see that there are a set ofsubroutines beginning with an underscore (for example, _known_hosts). These are the subroutines thatdo the work of figuring out the possible autocompletions. Search for a line that looks like this:complete -F _known_hosts traceroute ping telnet host sshcomplete -F identifies the function to use for completion. Anything returned by the function (here,known_hosts) is provided as a possible completion option for the commands listed after that function(here, traceroute ping telnet host ssh). -C is a similar option that executes a command and takes theoutput from that as possible completion. So, if you want to add your command to that list, just add it onthe end of this line and re-source the file:> vim /etc/bash_completioncomplete -F _known_hosts traceroute ping telnet host ssh mycommand> source /etc/bash_completionTest your command. It should now autocomplete with hostnames.■ Note In general, the functions in /etc/bash_completion fall back on the compgen bash built-in, which passesoptions to complete (another built-in) to try to generate a word completion. This is the “normal” tab-completionoption, but it’s quite limited for words such as hostnames using only the $HOSTFILE file, which will usually be/etc/hosts and which is very limited in modern systems that rely on external DNS.177Download at WoweBook.Com

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

Saved successfully!

Ooh no, something went wrong!