2. Fourth. Alias ​​(shell command)

xiaoxiao2021-03-06  58

IV. An alias is also a way to make the work is to use the command alias. Command alias is usually an abbreviation for other commands to reduce keyboard input. The command format is: alias [alias-name = 'original-command'] where alias-name is an alias for the user, oriGinal-command is the original command and parameters. It should be noted that because the Bash is a space or carriageway to identify the original command, if the quotation is not used, it may cause BASH to only intercept the first word, thereby incorporated. If any parameters are not used after the alias command, display the identified commands and alias that are currently being used. The alias taking the order is always valid during the login. If the user needs an alias to be valid at each login, then the alias command is written to the initialized script file. [Example] If you want to type the following command, it is best to build an alias for it to reduce workload. $ CD / USR / X11 / LIB / X11 If you create an alias called GoConfig, type the following command at the Bash prompt: $ alias goconfig = 'CD / usr / x11 / lib / x11' Now, unless You exit Bash, type GoConfig, will have the same role in the original long command. If you want to cancel an alias, you can use the following command: $ UNALIAS GOCONFIG This is some people who think useful, you can write them into initialized scripts to improve work efficiency: alias ll = 'ls -l'alias log =' Logout'Alias ​​ls = 'ls -f' If you are a DOS user and get used to the DOS command, you can define the following alias to make Linux performance like DOS: Alias ​​Dir = 'ls'alias copy =' CP'Alias Rename = 'mv'Alias ​​md =' mkdir'Alias ​​rd = 'rmdir' Note: When defining alias, the equal sign cannot have spaces on both sides, otherwise shell cannot decide what you need to do. Quotation is required only when the command is included in the command. If you type an alias command that does not have any parameters, all defined alias will be displayed. The prompt BASH has two levels. The first level prompt is the situation that the BASH that is often seen is waiting for the command to enter. The default value of the first level prompt is a symbol. If the user does not like this symbol, or willing to define the prompt himself, simply modify the value of the PS1 variable. For example, it will be changed to: ps1 = "Enter A Command:" The second level prompt is displayed when the BASH is required to enter more information to execute a command. The second level prompt defaults to>. If you need to define your prompt, you only need to change the value of the PS2 variable. For example, it will be changed to: ps2 = "more information:" The above two examples are the case where the set prompt is a static string. In fact, users can also use some special characters that have been defined in advance. These special characters will enable the prompt that contain information such as the current time. Table 10-4 lists some of the most common special characters and its meaning. Table 10-4 BASH prompt often uses special character special character descriptions! Show history number # Show the shell activation, the current command history number $ displays a $ symbol, if the current user is root, display # symbol / display an anti Slash D Displays the current date H Displays the computer host name n to run the shell, which will cause the prompt cross-line SHELL name T Dis display the current time u Displays the current user's username W to display the current work. Directory Benchmark W Display The current work directory These special characters can be combined to provide users with some prompts, providing very useful information.

Let's take a few practical examples: PS1 = "t" will make the prompt as follows: 02: 16: 15 and PS1 = T will make the prompt becomes as follows: T If ps1 = "t /" The prompt will be made as follows: 02: 16: 30 This example is the combination of two special characters. Controlling the operating mode BASH has some special variables that control the shell work in different ways. For example, variable NoClobber prevents accidentally overwriting a file unexpected when redirecting output. You can set the valid or invalidation of the NOCLOBBER variable through the set command. The set command has two parameters: one is an option to specify the variable open (ON) or off (OFF), one is a variable name of a special variable. To make a special variable (effective), use the -o option to make it (invalid), with O options. For example: $ set -o noclobber // Make Noclobber variables to set O Noclobber // Three most common shell special variables for Noclobber variables are: IgnoreEOF, NOCLOBBER, and NOGLOB. The IgnoreEofignoreEOF variable is used to disable the use of Ctrl D to exit Shell (Ctrl D is not only used to exit the shell, and the user can terminate the input to the standard output. This operation is often used in some shell utility commands, such as utility command CAT In these utility operations, it is very easy to operate and unexpectedly exits shell.ignoreEOF special variables are used to prevent this unexpected exit. After $ set -o ignoreeof, users can only exit with logout or exit commands. Shell.noClobbernoclobber variables can protect existing files when redirect output, prevent accidentally override. In the following example, the user sets NOCLOBBER to be valid. When redirects, the user tries to override the existing file MyFile, at which time the system An error message will be returned. [Example] $ set -o noclobber $ cat preface> myfilebash: myfile: Cannot Overwrite existing file $ noglob Set Noglob variables, the shell will not extend some special characters or strings in the file name. Such as characters *,?, [], Etc. will no longer be used as a wildcard. If the user wants to list file names answer ?, can pass the following steps: First, the user makes the Noglob variable to be invalid, and then list the file name. You can see At present, the question mark on the current command line is considered to be a character in the file name, instead of being regarded as a wildcard. $ Set -o noglob $ ls answer? Answer? Child shell with the export command User Log in to the Linux system, The system will start a user shell. In this shell, you can use the shell command or declared variable, you can also create and run the shell script. When you run the shell script, the system will create a child shell. At this time, there will be two A shell, one is the shell that is launched when logging in, and the other is the shell that the system creates by the system. When a scriptor is running, its script shell will terminate, you can return to the shell before executing the script. From this In terms of meaning, users can have many shells, each shell is derived from a shell (called the parent shell). The variables defined in the sub-shell are only valid within the sub-shell.

If a variable is defined in a shell script, this defined variable is just a local variable in the scripter, and the other shell cannot reference it. To make the value of a variable can be in other The shell is changed, you can use the export command to output the defined variable. The export command will cause the system to define a copy of this variable when you create each new shell. This process is called the variable output. [Example] In this example, the variable myfile is defined in the DISPFILE script. Then use the export command to output the variable myfile to any sub-shell, such as the child shell generated when the PrintFile script is executed. DispFile Script List: / ************** Begin Dispfile *********************** / MyFile = "List" Export Myfileecho "Displaying $ MyFile" Pr -T -n $ myfileprintfile / ************* * End Dispfile *************** / PrintFile script list: / ***** ********* Begin Printfile ************** / Echo "Printing $ myfile" Lpr $ myfile & / *********************** End PrintFile ********************* The custom BASH has already introduced many custom Bash methods in this section, but these methods are only for current. Bash dialogue is useful. As long as the user quits logins, all changes made will be lost. So you should do permanent modifications in the initialization file of the BASH. The user can put the commands you need to execute each time in the initialization file, and the most common command is the alias command and variable definitions. Each user in the system has a .bash_profile file in its primary directory, Bash will read the file each time, all commands included are executed. The following is the code of the default .bash_profile file: #. Bash_profile # get the aliases and functionsif [-f ~ / .bashrc]; the. ~ / .Bashrcfi # user specific Environment and startup programpath = $ PATH: $ homen / binenv = $ Home / .bashrcusername = "" "export username env pathps: faint !!!! it is Toooooooooooooo long !!!

转载请注明原文地址:https://www.9cbs.com/read-112944.html

New Post(0)