10 useful command line completion examples in Linux _linux shell

Source: Internet
Author: User

In a Linux system, when you enter a command, and then press two times the TAB key, all available commands that begin with the characters you enter are listed. It's not new, maybe you already know. This feature is called the command line complement bash completion. By default, the Bash command line can automatically complement the full file or directory name. However, we can enhance the Bash command complement function by complete command to reach a new height.

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, if you press the TAB key two times after you enter the Write command, the automatic completion feature provides a list of users who can 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 allow programmable command completion to function at your terminal, you only need to perform the following/etc/bash_completion:
#./etc/bash_completion

You can also cancel/ETC/BASH.BASHRC (from the Ubuntu Linux 13.04 system) as follows, so that you do not need to execute the above command:

 ### Enable bash completion in interactive shells
if! shopt-oq POSIX; then
 if [-f/usr/share/bash-completion/bas H_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 command-line completion

After you enable programmable command-line completion, you have some well-defined command completion features. The complete command is used to define command-line completion.
To view an existing command line complement, use the complete command as follows:
complete-p | less
the-p option in the example above is optional.

2. List the standard complement features in bash

By default, Bash provides the following standard complement functionality for Linux users.
1. Variable completion
2. Full User name
3. Complete the host name
4. Path completion
5. full file name completion

We discussed these in the previous bash standard complement.

3. Define a command name complement

All available commands can be used as a complement to a command with the-C option. In the example below, a complement is defined for the which command (LCTT: When you press the TAB key, you can list all the command names as fully available).

 $ complete-c which

$ which [Tab][tab]
Display all 2116 possibilities? (Y or N) 

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

4. Define a catalog complement

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

 $ ls
countfiles.sh dir1/     dir2/     dir3/

$ complete-d ls

$ ls [tab][tab]
dir1/     dir2/     dir3/ 

As above, even pressing TAB displays only the directory name.

5. Define a background task name complement

The complement function can also take the task name as the complement parameter. Option-j can define the task name as the 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. Complement with prefix and suffix

The complement function can define prefixes and suffixes for the actual complement. In the following example, the prefix and suffix of the complement are 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. With excluded file name and directory name complement

If the script is completed, 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 

If, as above, you want the completion of the LS command to ignore. 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 suffixes that are required to be excluded for automatic completion.

8. Through the IFS variable segmentation string to get full value
You can define a full value list through the-W option, and then slice through the IFS environment variable. The segmentation result expands the variable and displays it 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 split through the IFS separator, 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 generate a complement

You can introduce a function to define a complete complement. Use the-f option to pass the function name to the complete command to perform the function generation complement. 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:
1.COMPREPLY: The array controls the results displayed when the TAB is pressed
2.comp_words: This array contains the words entered by the command line
The index of the 3.comp_cword:comp_words array, used to differentiate the location of words that the command line can access
4.compgen:-W provides possible completion and parameters based on $current _arg

The function is placed in the Parser_option file and is introduced by the source command:
$ source Parser_option
Associate This 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: View the/etc/bash_completion to learn more about programmable complement functions.

10. When the first rule does not generate a result, it uses the second

You can use the-o option to generate a complement if the defined complement rule does not generate a match.
$ complete-f _count_files-o dirnames./countfiles.sh
As above, for./COUNTFILES.SH defines the _count_files complement function. If the _count_files () function does not generate any matches, the directory completion is triggered.

 $ ls 
countfiles.sh  dir1/   dir2/   dir3/

$./countfiles.sh [tab][tab]
dir1  Dir2 Dir3

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.