10 examples of command line completion in Linux

Source: Internet
Author: User

10 examples of command line completion in Linux

In Linux, when you enter a command, pressTABTo list all available commands starting with your input. This is not new. You may already know it. This function is called command line completion bash completion. By default, the bash command line can automatically complete the file or directory name. However, we can enhance the bash command complementing function throughcompleteCommand to bring it to a new height.

This tutorial illustrates how to use the programmable completion function to apply the Automatic completion function to options or command line parameters.

For examplewriteAfter the command, if you pressTABPress the button. The Automatic completion function is available for execution.writeList of users to operate on.

  1. $ write[TAB][TAB]
  2. bala raj
  3. jason randy
  4. john ritu
  5. mayla thomas
  6. nisha www-data

In the following exampletelnetCommand to display available host names:

  1. $ telnet [TAB][TAB]
  2. localhost dev-db fileserver

To enable the Programme command complementing function to work on your terminal, you only need to execute the following/etc/bash_completionYou can:

  1. #./etc/bash_completion

You can also cancel/etc/bash.bashrc(From Ubuntu Linux 13.04). In this way, you do not need to execute the above command:

  1. ### enable bash completion in interactive shells
  2. if! shopt -oq posix;then
  3. if[-f /usr/share/bash-completion/bash_completion ];then
  4. ./usr/share/bash-completion/bash_completion
  5. elif[-f /etc/bash_completion ];then
  6. ./etc/bash_completion
  7. fi
  8. fi

If you do not find the code/etc/bash_completionFile, you only need to useapt-getCommand to install the bash_completion package.

1. view existing command line completion

After the programmable command line complementing function is enabled, some defined command completion functions are available.completeCommand is used to define command line completion.

To view the existing command line completion, usecompleteCommand:

  1. complete -p |less

The-p option in the preceding example is optional.

2. List the standard complementing functions in bash

By default, Bash provides the following standard complementing functions for Linux users.

  1. Variable completion
  2. Username completion
  3. Host Name completion
  4. Path completion
  5. File Name completion

We have discussed this in the previous bash standard completion.

3. Define a command name to complete

The-c option can be used to complete all available commands as a command. In the following examplewhichThe command defines a completion (LCTT)TABAll command names can be listed as parameters that can be supplemented ).

  1. $ complete -c which
  2. $ which [TAB][TAB]
  3. Display all 2116 possibilities?(y or n)

If you press 'y', all command names are listed.

4. Define a directory completion

You can use option-d to define a complete parameter that only contains the directory name. In the following examplelsCommand defines completion.

  1. $ ls
  2. countfiles.sh dir1/ dir2/ dir3/
  3. $ complete -d ls
  4. $ ls[TAB][TAB]
  5. dir1/ dir2/ dir3/

As above, pressTABOnly the directory name is displayed.

5. Define a background Task Name to complete

The completion function can also use the task name as the completion parameter. Option-j defines the task name as a parameter passed to the command, as follows:

  1. $ jobs
  2. [1]-Stoppedcat
  3. [2]+Stoppedsed'p'
  4. $ complete -j ./list_job_attrib.sh
  5. $ ./list_job_attrib.sh [TAB][TAB]
  6. catsed

For background tasks, see examples in Linux background tasks to learn how to manage background tasks.

6. completion with a prefix and suffix

The completion function defines the prefix and suffix for the actual completion content. In the following example, the prefix and suffix of the supplemented content are defined for list_job_attrib.sh.

  1. $ jobs
  2. [1]+Stoppedcat
  3. $ complete -P '">'-S '<"'./list_job_attrib.sh
  4. $ ./list_job_attrib.sh [TAB][TAB]
  5. $ ./list_job_attrib.sh ">cat<"
7. Complete the excluded file names and directory names

After the script is run, the output directory is as follows:

  1. $ cd output/
  2. $ ls
  3. all_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt
  4. parser_mod.tmp extract.o

If you wantlsCommand completion to ignore the. tmp and. o files:

  1. $ export FIGNORE='.tmp:.o'
  2. $ complete -f -d ls
  3. $ cd output
  4. $ ls[TAB][TAB]
  5. all_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt

FIGNOREIs an environment variable that contains the filename suffixes to be excluded for Automatic completion.

8. Use the IFS Variable to split the string to obtain the completion value.

You can use the-W option to define the completion value list, and then useIFSSplit environment variables. The splitting result is displayed as a variable and displayed as a completion.

  1. $ export IFS=" "
  2. $ complete -W "bubble quick"./sort_numbers.sh
  3. $ ./sort_numbers.sh [TAB][TAB]
  4. bubble quick

As described aboveIFSAfter the Delimiter is split, the embedded variables are expanded as variable values, so you can use the following variables:

  1. $ echo $SORT_TYPE1
  2. bubble
  3. $ echo $SORT_TYPE2
  4. quick
  5. $ complete -W "$SORT_TYPE1 $SORT_TYPE2"./sort_numbers.sh
  6. $ ./sort_numbers.sh [TAB][TAB]
  7. bubble quick
9. Write a function to generate completion

You can introduce a function to define completion. Use the-F option to pass the function namecompleteCommand to execute the function to generate the completion content. For example, the function is as follows:

  1. _parser_options()
  2. {
  3. local curr_arg;
  4. curr_arg=${COMP_WORDS[COMP_CWORD]}
  5. COMPREPLY=( $(compgen -W '-i --incoming -o --outgoing -m --missed'-- $curr_arg ));
  6. }

In the above functions:

  1. COMPREPLYTABResult
  2. COMP_WORDS: the array contains the words entered by the command line.
  3. COMP_CWORD: The index of the COMP_WORDS array. It is used to distinguish the word locations that can be accessed by the command line.
  4. Compgen:-W provides possible completion and its parameters based on $ current_arg

This function is stored in the parser_option file and passedsourceCommand introduction:

  1. $ source parser_option

Associate this function with your parser. pl script:

  1. $ complete -F _parser_options ./parser.pl
  2. $ ./parser.pl [TAB][TAB]
  3. -i --incoming -o --outgoing -m --missed

As shown above, the parser. pl option is generated by the function _ parser_options.

Tip: View/etc/bash_completionTo learn more about programmable completion functions.

10. When the first rule does not generate a result, the second rule is used.

If the defined completion rule does not generate a match, you can use the-o option to generate a completion rule.

  1. $ complete -F _count_files -o dirnames ./countfiles.sh

As shown above, the _ count_files completion function is defined for./countfiles. sh. If the _ count_files () function does not generate any match, directory completion is triggered.

  1. $ ls
  2. countfiles.sh dir1/ dir2/ dir3/
  3. $./countfiles.sh [TAB][TAB]
  4. dir1 dir2 dir3

This article permanently updates the link address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.