10 useful command-line complement examples in Linux

Source: Internet
Author: User

In a Linux system, when you enter a command and press two TAB, all available commands that begin with the characters you enter are listed. It's not new, maybe you already know. This feature 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 completion function and let it reach new heights with the complete command.

This tutorial shows how we can use the programmable command line completion feature programmable completion to apply auto-completion to options or command-line parameters.

For example, if you press the TAB key two times after entering the write command, the AutoComplete feature provides a list of users to perform the write operation.

$ write [Tab][tab]

Bala Raj

Jason Randy

John Ritu

Mayla Thomas

Nisha Www-data

In the following example, you can display the available host names for the telnet command:

$ telnet [Tab][tab]

localhost dev-db fileserver

To make the programmable command completion function work in your terminal, you only need to perform/etc/bash_completion as follows:

# . /etc/bash_completion

You can also cancel the comments in the/ETC/BASH.BASHRC (from Ubuntu Linux 13.04 system), so that you don't need to execute the above command:

# # # Enable bash completion in interactive shells

if! 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

Fi

Fi

If you do not find the code and do not find the/etc/bash_completion file, you only need to install the Bash_completion package by using the Apt-get command.

1. View Existing command line completion

After enabling the programmable command line completion feature, there are already some well-defined command completion features. The complete command is used to define command line completion.

To view existing command line completion, use the complete command as follows:

Complete-p | Less

The-p option in the example above is optional.

2. list The standard completion features in bash

By default, Bash provides the following standard completion features for Linux users.

Variable completion

User name completion

Host name completion

Path completion

File name completion

We discussed this in the previous bash standard complements.

3. Define a command name completion

With the-C option, you can use all the available commands as a complement parameter for a command. In the following example, a complement is defined for the which command (LCTT: When you press TAB two, you can list all the command names as parameters to complement them).

$ complete-c which

$ which [Tab][tab]

Display all 2116 possibilities? (Y or N)

As above, if you press ' Y ', all the command names will be listed.

4. define a catalog completion

With option-D, you can define a complement parameter that contains only the directory name. In the example below, the completion is defined for the LS command.

$ ls

Countfiles.sh dir1/dir2/dir3/

$ complete-d ls

$ ls [Tab][tab]

dir1/dir2/dir3/

As above, double-clicking the tab will only display the directory name.

5. Define a background task name completion

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

$ jobs

[1]-Stopped Cat

[2]+ Stopped sed ' p '

$ complete-j./list_job_attrib.sh

$./list_job_attrib.sh [Tab][tab]

Cat SED

For background tasks, you can refer to the examples in the Linux background task to learn how to manage background tasks.

6. complements with prefixes and suffixes

The complement function defines the prefix and suffix for the actual complement content. In the following example, the prefix and suffix of the complement content is defined for 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 name completion with exclusions

If the script is finished, the output directory is as follows:

$ CD output/

$ ls

All_calls.txt incoming_calls.txt outgoing_calls.txt Missed_calls.txt

Parser_mod.tmp EXTRACT.O

As above, if you want the completion of the LS command ignore the. tmp and. o Files:

$ 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 an environment variable that contains the filename suffix that auto-completion needs to exclude.

8. the complement value is obtained by dividing the string with the IFS variable

You can define a complement value list with the-w option and then slice through the IFS environment variable. The slice results expand the variable and appear as a complement.

$ Export ifs= ""

$ complete-w "bubble quick"./sort_numbers.sh

$./sort_numbers.sh [Tab][tab]

Bubble Quick

As mentioned above, when a string is sliced through the IFS delimiter, the embedded variable is expanded to the variable value, so you can use the variable as follows:

$ echo $SORT _type1

Bubble

$ echo $SORT _type2

Quick

$ complete-w "$SORT _type1 $SORT _type2"./sort_numbers.sh

$./sort_numbers.sh [Tab][tab]

Bubble Quick

9. Write a function to create the completion

You can introduce a function to define the completion. Use the-f option to pass the function name to the complete command, and execute the function to generate the complement content. For example, the function is as follows:

_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: The array controls the results displayed when the TAB is pressed

Comp_words: The array contains the word entered by the command line

The index of the comp_cword:comp_words array used to differentiate the word locations that the command line can access

Compgen:-W provides possible complements and their parameters based on the $current _arg

The function is placed in the Parser_option file and introduced through the source command:

$ source Parser_option

Associate the function with your parser.pl script:

$ complete-f _parser_options./parser.pl

$./parser.pl [Tab][tab]

-I--incoming-o--outgoing-m--missed

As above, the parser.pl option is generated by the function _parser_options ().

Tip: Check out the/etc/bash_completion to learn more about the programmable complement function.

When the first rule does not produce a result, it uses the second

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

$ complete-f _count_files-o dirnames./countfiles.sh

As above, the _count_files complement function is defined for./countfiles.sh. If the _count_files () function does not generate any matches, it triggers the completion of the catalog.

$ ls

Countfiles.sh dir1/dir2/dir3/

$./countfiles.sh [Tab][tab]

Dir1 Dir2 Dir3

Free pick up brother even it education original Linux OPS engineer video/Detailed Linux tutorials, details Inquiry official website customer Service: http://www.itxdl.cn/linux/

or hooking up with Q2430675018.

Welcome to the Linux Communication Group 478068715

10 useful command-line complement examples in Linux

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.