Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize automatic completion command by Linux

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve automatic completion command in Linux". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve automatic completion command in Linux" can help you solve the problem.

The linux command automatically completes the centos7 and then restarts the system after executing yum install bash-completion.

After the docker command automatically completes yum install docker, there will be a file: / usr/share/bash-completion/completions/docker; obviously, the script is used for bash auto-completion, but after the script is executed directly by source, there will be an error when you type tab after the docker command.

It can be solved after yum install-y bash-completion. Kubectl command automatic completion kubectl this command line tool is very important, and there are many related commands, we can not remember so many commands, and often write mistakes, so command automatic completion is very necessary, kubectl command line tool itself supports complication, as long as a simple setting on it. The following are the setup commands for the linux system:

Source > ~ / .bashrcpython shell automatic command completes the creation file .pythonrc, adding the following

Import rlcompleter, readline readline.parse_and_bind ('tab:complete') appends the following to the end of the .bashrc file

Export PYTHONSTARTUP=~/.pythonrc updates the environment variable source. / .bashrc

Shell command completion in the shell command line can use the "Tab" key to automatically complete the command or file name, this function can actually be achieved in the shell script, so that the use of "Tab" key can automatically complete the custom shell function, there are three related commands, compgen, complete and compopt.

1 、 compgen

The compgen [option] [word] compgen command generates patches that may match word based on option and prints them to standard output. These options can be any option supported by the built-in command complete, but not "- p" and "- r". If "- F" or "- C" is used, the various shell variables set by the programmable completion function can still be used, but their values have no effect. Completion entries are generated in the same way, just as programmable completion code generates completion entries directly with the same completion options, and if word is specified, only the entries that match it are displayed.

2 、 complete

Complete [- abcdefgjksuv] [- o comp-option] [- DE] [- An action] [- G globpat] [- W wordlist] [- F function] [- C command] [- X filterpat] [- P prefix] [- S suffix] name [name.] The complete-pr [- DE] [name...] complete command specifies how to complete each name. If the option "- p" is specified or no option is specified, the existing completion method is printed out in a format that can be re-used as input. The option "- r" deletes the completion method of the specified name, and deletes the completion method of all names when no name is specified. The option "- D" means that subsequent options and actions will be applied to the default command completion, which means that previously undefined completion commands can also be completed. The option "- E" means that subsequent options and actions will be applied to empty command completion, that is, to complete blank lines. For options "- G", "- W", "- X", "- P" and "- S", parentheses should be used to prevent expansion before completion begins.

"- o bashdefault": if no completion entry is generated, other completion by default from bash is used.

"- o default": if no completion entry is generated, the default filename completion of "readline" is used.

"- o dirnames": if no completion entry is generated, the directory name completion is performed.

"- o filenames": tell "readline" to generate a file name for processing related to the file name, such as adding a slash to the directory name, referencing special characters, and removing the space at the end of the line for use in the shell function.

"- o noquote": tells "readline" that the file name is not referenced and will be referenced by default.

"- o nospace": tells "readline" not to add spaces after the completed name, but to add spaces by default.

"- o plusdirs": after the completion entry is generated, the directory name is completed and the results are added to the results of other actions.

"- An alias": alias, same option "- a". "- An arrayvar": array variable name. "- A binding": the "readline" key binds the name.

"- A builtin": shell built-in command name, same as "- b".

"- A command": command name, same as option "- c".

"- A directory": directory name, same as option "- d".

"- A disabled": the shell built-in command name that is not available.

"- An enabled": the available shell built-in command name.

"- An export": the exported shell variable name, same as the option "- e".

"- A file": file name, same as option "- f".

"- A function": shell function name.

"- A group": group name, same as "- g".

"- A helptopic": help topics supported by the built-in command help.

"- A hostname": hostname, obtained from the shell environment variable HOSTFILE.

"- A job": job name, same as "- j".

"- A keyword": shell reserved word, same option "- k".

"- A running": the name of the running job.

"- A service": service name, same as "- s".

"- A setopt": the parameter available for the option "- o" of the built-in command set.

"- A shopt": the option name acceptable to the built-in command shopt.

"- A signal": signal name.

"- A stopped": the name of the paused job.

"- A user": user name, same as "- u".

"- A variable": all shell variable names, same as "- v".

"- C command": executes the command in the child shell and takes the result as a completion entry.

"- F function": execute the function function in the current shell environment. When executing, the parameter "$1" indicates the name of the command that the parameter is completing, the parameter "$2" indicates the name of the completion, and the parameter "$3" indicates the word before the name of the completion, indicating that the completion entry is obtained from the array variable COMPREPLY at the end of execution. "- G globpat": extends using the file name extension mode globpat to generate possible completion entries.

"- P prefix": after all options are applied to the completion result, add the prefix prefix to the result.

"- S suffix": after all options have been applied to the completion result, add the suffix suffix after the result.

"- W wordlist": split the word list wordlist using the characters in the special variable IFS, and expand each word after the split. The entry that matches the word to be completed in the result is the completion entry.

"- X filterpat": filterpat is the pattern used for filename extension. It acts on the completion list generated by the previous options and parameters, and deletes each entry that matches the filter pattern. The leading exclamation mark in the pattern indicates no, and entries that do not match the filter pattern are deleted.

3 、 compopt

The compopt [- o option] [- DE] [+ o option] [name] compopt command modifies the completion options specified by each name, modifies the currently performed completion options if no name is specified, and displays each name or the current completion options if no options are specified. The possible values of the options are valid options for the built-in command complete above.

4, related variables in addition to the commands compgen, complete and compopt, shell command line automatic completion also uses some variables that come with shell, as follows.

COMP_CWORD: the subscript in the word "${COMP_WORDS}" that contains the current cursor position. This variable can only be used in programmable completed shell functions. COMP_LINE: currently on the command line, this variable can only be used in programmable completed shell functions.

COMP_POINT: the current cursor position is relative to the subscript at the beginning of the current command line. If the current cursor position is at the end of the current command line, the value of this variable is the same as "${# COMP_LINE}". This variable can only be used in programmable completed shell functions.

COMP_TYPE: an integer value corresponding to the type that attempts to complete when the completion function is triggered. The normal completion is "TAB", the completion list after entering tabs continuously is "?", the completion entry of other parts is listed as "!", the completion entry is listed as "@" without changing the word, and the completion menu is "%". This variable can only be used in programmable completion shell functions.

COMP_KEY: the key that triggers the current completion function, or the last key in the key sequence.

COMP_WORDBREAKS: the "readline" library uses word-delimited characters for word completion, and if this variable is not set, it will lose its special function even if it is reset later.

COMP_WORDS: an array variable that contains each word on the current command line, which, like "readline", is split into words by COMP_WORDBREAKS. This variable can only be used in programmable completed shell functions.

COMPREPLY: an array variable from which bash reads the completion entries generated by the shell function called by the programmable completion.

5. The example test script is as follows:

Function autotab () {echo "function autotab called $@"} autotab_list= ("aa"bb"cc"dd"123") function _ autotab () {local cur COMPREPLY= () cur=" ${COMP_ WORDS [comp _ CWORD]} "COMPREPLY= ($(compgen-W" ${autotab_list [*]} "--${cur})) return 0} complete-F _ autotab autotab example When executing the command autotab on the shell command line, if the command is not finished, pressing the Tab key will complete the command, and it will also display or complete some parameters, which are done through the command complete and the function _ autotab, and the elements of the array autotab_list will be expanded to the parameters of the command.

This is the end of the introduction of "how to achieve automatic completion command in Linux". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report