In Linux, enter a command and press the TAB key twice to list all available commands starting with the input character. This is not new. You probably already know this. This function is called command completion. By default, the bash command line can automatically complete the file or directory name label: Linux command
In Linux, enter a command and press the TAB key twice to list all available commands starting with the input character. This is not new. You probably already know this. This function is called command completion. By default, the bash command line can automatically complete the file or directory name. However, we can make the bash command line complete to execute more operations, and the completion command can make it the next brilliant.
This tutorial illustrates how to use the programmable completion function to apply the auto-completion function to options or command line parameters.
For example, if you press the TAB twice after entering the write command, the auto-completion function provides a list of write operations.
$ write [TAB][TAB]bala rajjason randyjohn ritumayla thomasnisha www-data
In the following example, enter the telnet command to display the available host names:
$ telnet [TAB][TAB]localhost dev-db fileserver
To enable the programmable command complementing function to work on your terminal, you only need to execute/etc/bash_completion, as shown below:
# . /etc/bash_completion
You can also cancel the comments under/etc/bash. bashrc (for Ubuntu Linux 13.04) so that you do not need to execute the above command,
enable bash completion in interactive shellsif ! shopt -oq posix; then if [ -f /usr/share/bash-completion/bash_completion ]; then . /usr/share/bash-completion/bash_completion elif [ -f /etc/bash_completion ]; then . /etc/bash_completion fifi
If you do not find the code or the/etc/bash_completion file, you only need to install the bash_completion package by using the apt-get command.
1. view the existing bash completion command
By enabling the programmable bash command line complementing function, you can define a set of bash completion commands. Command line completion can be used to define bash completion commands.
Let's take a look at the existing bash complementing function and use the complete command as follows:
complete -p | less
Option-p is optional.
2. list of standard completions in Bash
Bash provides the following standard completion commands for linux users by default.
- Variable name completion (Variablename completion)
- Username completion)
- Host name completion)
- Pathname completion)
- Filename completion)
We have discussed this in an earlier article bash standard completion.
3. define completion commands for obtaining commands
Use the-c parameter to define a completion command to obtain the list of available commands. In the following example, a completion command is defined for the which command,
$ complete -c which$ which [TAB][TAB]Display all 2116 possibilities? (y or n)
As shown above, if you press "y", all commands will be displayed.
4. define the completion command for obtaining the Directory
Use the parameter d to define a completion command that only obtains the directory name. in the following example, the completion command of ls is defined.
$ lscountfiles.sh dir1/ dir2/ dir3/$ complete -d ls$ ls [TAB][TAB]dir1/ dir2/ dir3/
As shown above, you can view the directory name by pressing the TAB twice in a row.
5. obtain the completion command for obtaining the background job name
You can use the complete command to obtain the job name as a parameter. The parameter j is used to pass the job name as a parameter to the command line, as shown below:
$ jobs[1]- Stopped cat[2]+ Stopped sed 'p'$ complete -j ./list_job_attrib.sh$ ./list_job_attrib.sh [TAB][TAB]cat sed
To learn more about background tasks, you can use these cases to learn how to manage Linux background tasks.
6. use the prefix and suffix to complete the command
The completion command can be defined by prefix (added later) and suffix (added later. In the following example, the prefix and suffix are defined in list_job_attrib.sh.
$ jobs[1]+ Stopped cat$ complete -P '">' -S '<"' ./list_job_attrib.sh$ ./list_job_attrib.sh [TAB][TAB]$ ./list_job_attrib.sh ">cat<"
7. file name and directory completion with exclusion function
Take a look at the following script and output the files under the output Directory:
$ cd output/$ lsall_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txtparser_mod.tmp extract.o
In the above example, if you want to exclude files suffixed with. tmp and. o, you can implement the automatic completion function of the ls command as follows:
$ export FIGNORE='.tmp:.o'$ complete -f -d ls$ cd output$ ls [TAB][TAB]all_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt
FIGNORE is a shell variable that contains the suffix of the file name excluded from the auto-completion queue.
8. use the IFS variable to split the String to obtain the split value.
The word table can be split into multiple words by using the w parameter by the string defined in the IFS variable. Each word is separated and displayed.
$ export IFS=" "$ complete -W "bubble quick" ./sort_numbers.sh$ ./sort_numbers.sh [TAB][TAB]bubble quick
As mentioned above, words are extended after being split by IFS, so the variables shown below may also exist.
$ echo $SORT_TYPE1bubble$ echo $SORT_TYPE2quick$ complete -W "$SORT_TYPE1 $SORT_TYPE2" ./sort_numbers.sh$ ./sort_numbers.sh [TAB][TAB]bubble quick
9. write your own functions to implement automatic complementing
You can declare a function to define the completion function. Use the-F parameter to specify the name of the function that is passed into the completion command. For example, a function can be written as the following style.
_parser_options(){ local curr_arg; curr_arg=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W '-i --incoming -o --outgoing -m --missed' -- $curr_arg ) );}
In the above function,
- COMPREPLY: an array of information that is stored after pressing [TAB] [TAB.
- COMP_WORDS: array of words entered in the command line
- COMP_CWORD: the index of the COMP_WORDS array, which can be used to access words that are not used in the command line.
- Compgen: use the-W parameter to hold as complete and separate content as possible in the current_arg variable.
The parser_option function in the file is executed as follows through source:
$ source parser_option
This function is linked to the script parser as follows:
$ complete -F _parser_options ./parser.pl$ ./parser.pl [TAB][TAB]-i --incoming -o --outgoing -m --missed
As shown above, parser parameters can be generated through the _ parser_options function.
Note: Check the/etc/bash_completion file to learn more programmable command line complementing functions.
10. when the first specification does not match, the second specification needs to be executed.
If no matching is performed through the defined completion specification, the completion defined by the-o parameter is executed.
$ complete -F _count_files -o dirnames ./countfiles.sh
Same as above, the completion defined by the _ count_files function defined in the _ count_files file is used. if the _ count_files function does not match, directory completion is executed.
$ ls countfiles.sh dir1/ dir2/ dir3/$./countfiles.sh [TAB][TAB]dir1 dir2 dir3
Original article: Balakrishnan Mariyappan