10 of the commands you don't know in Linux are padded skills

Source: Internet
Author: User
Tags sort

By default, the Bash command line can automatically complement the full file or directory name. However, we can do more with the Bash command line complement and make it the next glory by making a full command.

This tutorial explains how we use the programmable command-line complement (programmable completion) to apply the automatic completion function to options or command-line arguments.

For example, after you enter the Write command, if you press the TAB key two times, the Auto Fill feature provides a list of the write actions to be performed.

The code is as follows:

$ write [Tab][tab]

Bala, Raj.

Randy, Jason.

John Ritu

Mayla Thomas

Nisha Www-data

In the following example, entering the Telnet command will display the host name available:

The code is as follows:

$ telnet [Tab][tab]

localhost dev-db fileserver

To allow programmable command completion to work at your terminal, you only need to perform/etc/bash_completion, as shown below:

The code is as follows:

# . /etc/bash_completion

You can also cancel/ETC/BASH.BASHRC (for the Ubuntu Linux 13.04 system), so that you don't have to execute the above command

The 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 don't find the code and you don't find the/etc/bash_completion file, you just need to install the Bash_completion package by using the Apt-get command.

1. View existing Bash completion commands

Enable programmable Bash command line completion to define a set of bash completion commands. Command-line completion can be used to define a bash completion command.

Take a look at the existing Bash completion feature, using the full command, as follows:

The code is as follows:

Complete-p | Less

Option-P is optional.

2. List of standard complements in bash

BASH provides the following standard completion commands for Linux users by default.

Variable name complement (Variablename completion)

User name complement (Username completion)

Host name complement (Hostname completion)

Path-Path completion (Pathname completion)

FileName complement (filename completion)

3, to obtain the command to define the complete command

Use the-c parameter to define a complement command to get a list of the commands you can use. In the following example, a completion command is defined for the which command.

The code is as follows:

$ complete-c which

$ which [Tab][tab]

Display all 2116 possibilities? (Y or N)

As you can see above, if you press "Y", all the commands will be displayed.

4, to obtain the catalog definition complete the command

Using parameter D, define a completion command that only obtains the directory name, in the following example, the completion command for LS is defined

The code is as follows:

$ ls

Countfiles.sh dir1/dir2/dir3/

$ complete-d ls

$ ls [Tab][tab]

dir1/dir2/dir3/

As you can see above, press TAB two consecutive times to see the directory name.

5, to obtain the background job name get complete command

By using the complete command, obtaining the job name as a parameter is allowed. Parameter j is used to pass the job name as an argument to the command line, as shown below:

The 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 examples to understand how to manage Linux background tasks.

6. Use prefix and suffix complement command

The complement command can be defined by the prefix (added later) and the suffix (added later). In the following example, the prefix and suffix are defined in the list_job_attrib.sh.

The 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, with the exclusion of the file name and directory completion

Take a look at the script below to output the file below the export directory:

The 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 with the. tmp and. o suffix, implement the automatic completion of the LS command, you can do this:

The 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 that is excluded from the automatic completion queue.

8, through the IFS variable segmentation string string, get the value of the split.

The word list can be separated into multiple words by using the W parameter by a string defined in the IFS variable. Eventually each word will be separated and shown.

The code is as follows:

$ Export ifs= ""

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

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

Bubble Quick

As mentioned above, the word is expanded after being partitioned by IFS, so there may also be these variables shown below.

The 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 function to achieve automatic completion function

You can declare a function to define the complement function. Using the-f argument, the function name that is passed into the complement command can be executed and. For example, a function can be written in the following style.

The 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 printed information that is stored after [Tab][tab] is pressed.

2.comp_words: An array of words entered at the command line

An index of the 3.comp_cword:comp_words array that can access words that are not in place in the command line.

4.compgen: Use the-w parameter to hold the contents as complete and separate as possible in the CURRENT_ARG variable.

The Parser_option function in the file is executed by source as follows:

The code is as follows:

$ source Parser_option

This function links to the script parser as follows:

The code is as follows:

$ complete-f _parser_options./parser.pl

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

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

As seen above, the parser's parameters can be generated by the _parser_options function.

Note: View the/etc/bash_completion file to learn more about programmable command-line complement functions.

10, when the first specification does not match, you need to implement the second specification

The completion defined by the-o parameter executes if no match is made through the defined completion specification.

The code is as follows:

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

Ibid., by using the completion defined in the _count_files function defined in the _count_files file, if the _count_files function does not match, then the directory completion is performed.

The code is as follows:

$ ls

Countfiles.sh dir1/dir2/dir3/

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

Dir1 Dir2 Dir3

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.