Tips for completing 10 unknown commands in Linux

Source: Internet
Author: User
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.


Copy codeThe code is as follows:
$ Write [TAB] [TAB]
Bala AJ
Jason randy
John ritu
Mayla thomas
Nisha www-data

In the following example, enter the telnet command to display the available host names:


Copy codeThe code is as follows:
$ 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:


Copy codeThe code is as follows:
#./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,


Copy codeThe code is as follows:
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 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:

Copy codeThe code is 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)

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,

Copy codeThe code is as follows:
$ 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.


Copy codeThe code is as follows:
$ Ls
Countfiles. 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:

Copy codeThe code is as follows:
$ 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.

Copy codeThe code is as follows:
$ 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:


Copy codeThe code is as follows:
$ Cd output/
$ Ls
All_calls.txt incoming_calls.txt outgoing_calls.txt missed_calls.txt
Parser_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:


Copy codeThe code is 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.


Copy codeThe code is as follows:
$ 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.


Copy codeThe code is 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 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.


Copy codeThe code 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,

1. COMPREPLY: an array of information that is stored after pressing [TAB] [TAB.
2. COMP_WORDS: array of words entered in the command line
3. COMP_CWORD: the index of the COMP_WORDS array. you can access words that do not need to be placed in the command line.
4. compgen: use the-W parameter to hold as full and separate content as possible in the current_arg variable.
The parser_option function in the file is executed as follows through source:

Copy codeThe code is as follows:
$ Source parser_option

This function is linked to the script parser as follows:

Copy codeThe code is 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.


Copy codeThe code is as follows:
$ 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.

Copy codeThe code is as follows:
$ Ls
Countfiles. sh dir1/dir2/dir3/
$./Countfiles. sh [TAB] [TAB]
Dir1 dir2 dir3

Related Article

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.