Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Linux \ Linux 10 useful examples of command-line completion     - CentOS 6.5 using Virtualenv under development environment to build Python3 (Linux)

- shell script error dirname: invalid option - b (Database)

- Go powerful development server simple example (Server)

- 20 Advanced Java interview questions summary (Programming)

- Download Google Android source code under Ubuntu 4.4 (Linux)

- Hive start being given: Found class jline.Terminal, but interface was expected (Database)

- Configuring the remote Linux server SSH key authentication to automatically login in Mac OS X (Server)

- ORA-00824: can not set sga_target due to existing problem-solving (Database)

- Linux firewall settings -DNS server articles (Server)

- Offline (local) Yum source structures (Linux)

- CentOS7 Minimal minimize installation and then install the GNOME graphical interface (Linux)

- How the program is executed (Programming)

- Single-node Hadoop environment to build (Server)

- Oracle conditional select statements and looping statements (Database)

- Oracle View Object Information (Database)

- Linux IO redirection (Linux)

- Linux administrator should command: sed and awk (Linux)

- RabbitMQ tutorial examples: RabbitMQ installation under Windows (Linux)

- Use GNU / Linux broadcasting of television programs (Linux)

- Android Studio Getting Started Hello World (Programming)

 
         
  Linux 10 useful examples of command-line completion
     
  Add Date : 2018-11-21      
         
       
         
  On Linux systems, when you enter a command, and then press the TAB key twice, you will list all available commands starting with the entered characters. This is not new, you may already know. This feature is called command line completion bash completion. By default, bash command line, you can auto-complete file or directory name. However, we can enhance the bash command completion function by complete command it to new heights.

This tutorial explains how we use programmable command line completion (programmable completion) is applied to the auto-completion option or command line parameters.

For example: After entering the write command, if you press the TAB key twice, auto-complete feature will provide a user list for the implementation of write operations.

$ 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
Make programmable command completion feature in your terminal to work, you only need to perform the following / etc / bash_completion to:

#. / Etc / bash_completion
You can also cancel /etc/bash.bashrc (from Ubuntu Linux 13.04 system) in the following notes, so you may not 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, but could not find / etc / bash_completion file, then you only need to use apt-get command to install the package to bash_completion.

1. Review existing command-line completion

After enabling a programmable command line completion, we already have some well-defined command completion. complete command-line completion used to define commands.

To view an existing command line completion, use the following command to complete:

complete -p | less
In the above example -p option is optional.

2. List the standard bash-completion

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

Variable Completion
Username completion
Hostname completions
Path completion
Filename completion
We discussed these in the previous bash completion of the standard.

3. Define a command name completion

-c Option through all of the available commands can be used as a command parameter completion. In the example below it, which is a defined command completion (LCTT Annotation: In double-TAB, you can list all the command name as a full complement of parameters).

$ Complete -c which
$ Which [TAB] [TAB]
Display all 2116 possibilities? (Y or n)
As above, if you press 'y', it will list all the command name.

4. Define a directory completion

The option -d, you can define a directory name that contains only the completion argument. In the following example, the ls command defines completion.

$ Ls
countfiles.sh dir1 / dir2 / dir3 /
$ Complete -d ls
$ Ls [TAB] [TAB]
dir1 / dir2 / dir3 /
As above, even pressing TAB will only display the directory name.

5. Define a background task name completion

Completion feature also can be used as the name of the task completion argument. Option -j can define the task name as a parameter passed to the command, as follows:

$ Jobs
[1] -Stoppedcat
[2] + Stoppedsed'p '
$ Complete -j ./list_job_attrib.sh
$ ./list_job_attrib.sh [TAB] [TAB]
catsed
About background tasks, you can refer to the Linux background task examples to learn how to manage background tasks.

6. with prefixes and suffixes complement

Completion feature can complement the actual content is defined prefix and suffix. In the following example, in order to define the completion list_job_attrib.sh content of prefixes and suffixes.

$ Jobs
[1] + Stoppedcat
$ Complete -P ' ">' - S '<"' ./ list_job_attrib.sh
$ ./list_job_attrib.sh [TAB] [TAB]
$ ./list_job_attrib.sh "> Cat <"
7. The file and directory names with exclusion completions

If the script is run after the completion of the output directory 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 to ignore the ls command completion .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 auto-complete as to exclude file name suffix.

8. Adoption of the IFS variable split the string to get the full complement value

-W Option can be defined by a list of up to the full value, and then through IFS environment variable segmentation. Segmentation results are variable and as a complement to expand the display.

$ Export IFS = ""
$ Complete -W "bubble quick" ./ sort_numbers.sh
$ ./sort_numbers.sh [TAB] [TAB]
bubble quick
After As described above, the string delimiter by IFS segmentation, embedded variables will be expanded to the variable value, so you can use the following variables:

$ 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 completion

You can introduce a function defined completion. Use the -F option to complete the function name passed to the command, execute function generation completion content. For example, the following functions:

_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:

Results The array control even press TAB display: COMPREPLY
COMP_WORDS: This array contains the command line input word
COMP_CWORD: Index COMP_WORDS array, use the command line to distinguish it accessible position of the word
compgen: -W-based $ current_arg offer possible completions its parameters
The functions in parser_option file and introduced through the source command:

$ Source parser_option
The function and your script parser.pl associate:

$ Complete -F _parser_options ./parser.pl
$ ./parser.pl [TAB] [TAB]
-i --incoming -o --outgoing -m --missed
Above, parser.pl options by function _parser_options () generates.

Tip: Check / etc / bash_completion to learn more programmable completion functions.

10. When the first rule does not produce results, then use the second

If the rules do not define the completions generate match, you can use the -o option to generate completion.

$ Complete -F _count_files -o dirnames ./countfiles.sh
As for ./countfiles.sh defined _count_files completion function. If the _count_files () function does not generate any matches, it would trigger the directory completion.

$ Ls
countfiles.sh dir1 / dir2 / dir3 /
$. / Countfiles.sh [TAB] [TAB]
dir1 dir2 dir3
     
         
       
         
  More:      
 
- Ubuntu 14.04 install PostgreSQL 9.2 (Database)
- Ubuntu 15.04 installed JDK and configured as the default JDK (Linux)
- Memcached and Redis (Linux)
- Use IF NOT EXISTS create a data table (Database)
- Django template inheritance child (Programming)
- Java data structures - the linear form of the single-chain applications (Programming)
- Lucene Getting Started Tutorial (Server)
- RHEL 7.1 compile and install Ganglia 3.7.1 (Server)
- Detailed steps - GAMIT solver (Linux)
- Using C / C ++ extensions Python (Programming)
- Use PuTTY to access the virtual machine Linux under Windows xp (Programming)
- When Vim create Python scripts, vim autocomplete interpreter and encoding method (Programming)
- Linux process management related content (Linux)
- Grub2: Save Your bootloader (Linux)
- Linux System Tutorial: Fix ImportError: No module named wxversion error (Linux)
- C ++ Supplements - locates the new expression (Programming)
- MySQL High Availability plan several options (Database)
- Four levels to deal with Linux server attacks (Linux)
- Linux file permissions bit forced bits and adventure Comments (Linux)
- Using Maven to download Spring (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.