Basic analysis of Linux operating system (vii)--bash (Shell) Basics (2)

Source: Internet
Author: User
Tags arithmetic clear screen


Iii. Command History
Bash has absorbed a lot of essence from the Korn shell and the C shell, one of which formally holds the feature of a cached copy of the executed command, which we call the "command history" feature.
Why should we use the command history feature?
After each user login succeeds, especially after login with file interface such as bash, all operations are implemented by executing commands, it is inevitable to repeat a command, if each command depends on the type of input, is not a problem, But inefficient and a "shameful" act of waste of time, order history provides us with a way to solve this problem.
The preservation of the command history is divided into two stages:
1. After bash is turned on, each executed command is temporarily cached in memory in a space specifically designated to cache historical commands;
2. Before bash closes, bash is always actively trying to save historical commands in the In-Memory history command cache to the. bash_history file in the user's home directory.
In order for the user to be able to use the history command that was executed before the last bash shutdown after logging in again, bash loads all of the content saved in the. bash_history file in the user's home directory into memory during user authentication by opening bash. This way, when the user has just logged on to the system, the command history in the memory cache and the history command saved in the. bash_history file are exactly the same until you type a new command.
To do this, bash also prepares or defines several environment variables to describe the relevant characteristics of the command history, including:
Histsize: Defines the number of history commands that can be cached in the memory cache, with a default value of 1000;
Histfile: Defines the file that saves the user history command on disk, the default is ~/.bash_history;
Histfilesize: Defines the maximum number of historical commands that are stored in a command history file, with a default value of 1000;
Histtimeformat: Displays the time format of the history command, which can be extended to show more information, which is undefined by default;
Histcontrol: Control history Command Record way, have ignoredups, Ignorespace, ignoreboth three value optional, default value is ignoredups;
Histignore: According to the defined characteristics, choose which type of command is not stored in the command history, default is undefined;

So how do you see the history command cache in memory? We can use the history command to do this.
History
#: Show Recent # History
-A: Append the newly executed command history list to the history file for this session
-C: Empty the history command record list in memory
-D: Deletes the command specified in the history;
-N: Read unread rows to History list in history file
-P: Expands the history parameter into multiple lines, but does not exist in the history list
-r: Attach all History commands from the history command file to the history command record list of memory
-S: Expands the history parameter into a row, appended to the history list
-W: Save History Command record list to history command file (. bash_history)

You can use! To invoke the cached content in the history command cache as follows:
!#: A command that repeats a specific ID number in a History command record list
!: Repeat the last command
!-#: Repeat the countdown # command
!string: Repeats the last command in the history command record list that starts with string

!^: The first parameter of the last command
!$: Last parameter of the last command note: "!$" = "ESC,." = "ALT +."
!*: All parameters of the last command
!:#: The first # parameter of the last command

!#:^: Specifies the first parameter of a numbered history command
!#:$: Specifies the last parameter of a numbered history command
!#:*: Specify all parameters of the numbered history command
!#:#: Specify the # parameter of the numbered history command

! str:^: Specifies the first argument of a history command at the beginning of a string
! str:$: Specifies the last parameter of the history command at the beginning of the string
! str:*: Specifies all parameters of the history command at the beginning of the string
! str:#: Specify the # parameter of the history command at the beginning of the string

Bash's shortcut keys
Ctrl+l clear screen, equivalent to clear command
CTRL + C cancels the execution of the command
Ctrl-a will move to the front of the command line
CTRL-E is moved to the last side of the command line
Ctrl-u will be deleted to the beginning
Ctrl-k will be deleted to the end of the line
Ctrl-arrow Ctrl-b Ctrl-f moves one character to the left or right
Esc-b one word to the left
Esc-f Move Right One word

Four, Shell syntax
Simple command:
A simple command is a sequence of words separated by whitespace characters and redirected, and an optional variable that uses the control operator to end the assignment. The first word indicates the command to execute, which is taken as the No. 0 parameter. The rest of the words are treated as arguments to this command.
The return value of a simple command is its exit state, or if the command is terminated by Signal N, the return value is 128+n.

Pipeline:
The pipe is the controlled character "|" or "|&" separates the sequence of one or more commands, the format of the pipeline is this:
[Time [-P]] [ ! ] command [| Command2 ...]
The standard output of command commands is connected to the standard input of the command Command2 through the pipeline. Such a connection is made before any redirection is specified by the command.
If standard errors using |&,command are piped to the standard input of the Command2, this is the shorthand form for 2>&1 | The redirection of this implied standard error is done after any redirection is specified by the command.
The exit status of the pipeline is the exit status value of the last command unless the Pipefail option is turned on. If Pipefail is turned on, the return status of the pipeline is: 0 if all commands are successfully exited, otherwise the pipe state return value is the non-0 status return value of the last command. If the word is reserved! The exit status of the pipe is the logical non-result of the exit state as described above before being used in a pipeline. The shell waits until all the commands in the pipeline have finished executing before returning to the exit status value.
The user and system time that the execution pipeline consumes is reported when the pipe is aborted, if the reserved word is used before the pipeline. The-P option will make the output conform to the POSIX-specified format. The TimeFormat variable can be set to a formatted string that specifies how the time information should be displayed.
Each order in the pipeline is executed as a separate process

Sequence:
The list (which can be translated here as a sequence) is one or more operators ";", "&", "&&" or "| |" Delimited sequence, and you can choose to end with a character such as ";", "&" or "<newline>".
In these sequence operators, the "&&" and "| |" The same priority, followed by ";" and "&", their priority is the same.
A sequence can have one or more newline characters separating the commands, instead of separating them with semicolons.
If a command is terminated by the control operator "&", the shell executes the command in a child shell in the background. The shell does not wait for the command execution to end, and the return state is always 0. With a semicolon ";" Delimited commands are executed sequentially; the shell waits for each command to end sequentially. The return status is the return state of the last command executed.
Control operator "&&" and "| |" Each represents the "with" and "or" sequences respectively. The form of the "and" sequence is:
Command1 && Command2
Command2 is only executed when Command1 returns 0 o'clock.
The "or" sequence is in the form of:
Command1 | | Command2
Command2 is executed only if Command1 returns a non-0 state. The return status of the and and or sequences is the return state of the last command executed in the sequence.

Compound command
Compound command (compound commands) is one of the following:
(list):
"List" will be executed in a child shell environment. Variable assignments and built-in commands that affect the shell environment variables will no longer work after the command executes. The status return value is the status return value of the command represented by "list".
{list;}
"List" will be executed in the current shell environment. "List" must end with a newline character or semicolon. This practice is also known as a command group. The status return value is the status return value of the command represented by "list". Note Unlike metacharacters "(" and ")", "{" and "}" are reserved words and must appear when the reserved word is allowed to be recognized. Since they do not produce word breaks, they must be separated from the "list" by a space.
((expression))
According to the rules described in the "Arithmetic EVALUATION" section below, "expression" is evaluated. If the value of "expression" is a value other than "0", the status return value is 0, otherwise the return status is 1. This is actually the equivalent of let "expression".
[[Expression]]
Returns a status value of "0" or "1", based on the condition expression "expression" evaluation. "Expression" consists mainly of descriptors of conditional expressions. The words "[[" and "]" do not break the word and the path expands, while the wavy lines expand, the parameters and variables expand, the arithmetic expands, the command substitution, the process substitution, and the removal of the references are all executed. Conditional operators such as-F must dereference to be recognized as a live descriptor.
When using the "[[", "<" and ">" Operators, the current environment logic is used to sort the order.
When using the "= =" and "! =" operators, the string to the right of the operator is considered to be a pattern that matches according to the rules of the pattern match. If the shell turns on the Nocasematch option, it performs a case-insensitive match. If the match, the return value is 0, otherwise 1 is returned. All parts of the pattern should be referenced, forcing it to match as a string.
There is also an additional binary operator "=~", which, if available, has the same precedence as "= =" and "! =". When this operator is used, the string to the right of the operator is matched as an expanded regular expression. If the match, the return value is 0, otherwise 1 is returned. If the regular expression has a syntax error, the return value of the conditional expression is 2. If the shell turns on the Nocasematch option, it performs a case-insensitive match. All parts of the pattern should be referenced, forcing it to match as a string. Substrings are matched by parentheses that are held in the array variable bash_rematch. An element in the array variable Bash_rematch that uses the subscript "0" is part of the string that matches the entire regular expression. An element in an array variable Bash_rematch that uses subscript "n" is part of a string that matches the nth occurrence of the sub-expression in parentheses.
An expression can be combined with the following operators. The descending sequence according to the priority is as follows:
(expression)
Returns the value of expression "expression". Parentheses can be used to raise the precedence of an operator.
! Expression
If expression "expression" returns false, the entire expression returns TRUE.
Expression1 && expression2
If both the expression expression1 and expression2 return true, the entire expression returns TRUE.
expression1 | | Expression2
If the expression "expression1" or "expression2" is at least one return true, the entire expression returns TRUE.
If expression1 can determine the return value of an entire conditional expression, the && and | | The expression2 operator does not evaluate an expression.

for name [[Word ...];] Do list; Done
A series of words after "in" is expanded to produce a list. The variable "name" is assigned to each element of the list in turn, and "list" is executed every time. If "in Word" is skipped, then the "for" command iterates through each set of positional parameters. The return status is the exit state of the last command executed. If the result of the subsequent word expansion is an empty list, no command is executed and the status is returned as "0".
for (EXPR1; expr2; expr3); Do list; Done
First, the arithmetic expression "EXPR1" is evaluated according to the rule. The arithmetic expression "EXPR2" is evaluated repeatedly, knowing that the evaluation result is "0". Each time a "expr2" evaluates to nonzero, the "list" is executed and the arithmetic expression "EXPR3" evaluated. If any expression is skipped, it is considered the execution result is "1". The return value is the return value of the last command executed in "list" or the return value is False if any one of the expressions is invalid.
Select name [in Word]; Do list; Done
A series of words after "in" is expanded to produce a list of items. The expanded Word collection is output to the standard error, preceded by a number. If "in Word" is skipped, the position parameter is output. At that point, the PS3 prompt is displayed and a row is read from the standard input. If the row includes a number that corresponds to the display "words", then the value of the "name" variable is set to "word". If you enter a blank line, Word and the prompt are displayed again. If an EOF is read, the command ends. Any other value being fetched will cause the value of the variable name to be set to null. The rows that are read are saved in the variable reply. "List" executes after each selection until a break command is executed. The exit status of the Select command is the exit state of the last command executed in "list", if no execution command is 0.
case word in [[(] pattern [| pattern] ...) list;;] ... Esac
The case command expands word first, then attempts to use each pattern in turn to match it, using the same matching rules as the path expansion. Word uses wavy lines to expand, parameters and variable expansion, algorithm substitution, command substitution, procedure substitution, and elimination of references. Each "pattern" also uses wavy lines to expand, parameter and variable expansion, algorithm substitution, command substitution, and process substitution. If the shell turns on the Nocasematch option, it performs a case-insensitive match. When a match is found, the corresponding "list" is executed. If found using the ";;" operator, after a match is found, no subsequent matches are attempted. Use ";&" to replace ";;", which allows "list" to be associated to the next pattern set to continue execution. Use ";; & "Replace";; ", which causes the shell to test the next Pattern list using a statement. If so, execute a successful match that has been associated to "list". If no pattern can match, the return value is 0. Otherwise, returns the return value of the last command executed in "list".
If list; then list; [Elif list; then list;] ... [Else list;] Fi
The "If List" is executed. If the exit status is 0, then list will be executed. Otherwise, each "elif list" is executed sequentially, and if the exit status is 0, the corresponding "then list" is executed and the command ends. Otherwise, the "Else list" will be executed if "else list" is present. The exit status is the exit state of the last command executed, or if all conditions are not met, the exit status is 0.
While list; Do list; Done
Until list; Do list; Done
The while command continuously executes the "Do List" until the last command in "list" exits with a status of 0. The until command is equivalent to the while command, except that the test of the condition is the opposite; executes the "Do List" until the last command in the sequence returns a non-0 status value. The exit status of the while and until commands is the exit state of the last command in the Do list, or if no command is executed, the exit status is 0.

Co-processing
Coprocess (co-processing) is a reserved word in front of the shell command using Coproc. The coprocessor executes asynchronously in the child shell, just as the command uses the end of the "&" control to establish a connection between the execution shell and the coprocessor using a two-way pipeline. The co-processing format is as follows:
Coproc [NAME] command [redirections]
This creates a co-processing named "name". If "name" is not specified, then the default name is "Coproc". If "command" is to give a simple command, "NAME" does not have to be specified; otherwise, it will be interpreted as the first word of this simple command. When Coproc is executed, the shell creates an array variable named "name" in the context of executing the shell. The standard output of "command" is connected by a pipe to a file descriptor inside the running shell, which is assumed to be name[0]. The standard input of "command" is connected to a file descriptor of the running shell through a pipe, which is assumed to be name[1]. This pipeline is built before any commands are specified for redirection. The file descriptor is used as a parameter to provide to the shell command, and redirects are expanded using the standard word. As the value of the variable name_pid, the process ID of the shell-open execution coprocessor is available. The wait built-in command is used as a wait for the coprocessor process to terminate.
The return status of the coprocessor is the exit state of the command.

Shell function definition
A shell function is an object. This object can be understood to be one or several simple commands, or a series of compound commands with the new optional parameters are defined in advance, and when this function is called, all commands in the function are executed. The shell functions are defined in the following way:
[function] Name () {Compound-command} [redirection]
This allows you to define a function named "name". The reserved character function is optional. If we give this function reserved word, the parentheses are optional. The body of a function is a compound command. Those commands are usually contained in a compound command between "{" and "}". The entire compound command will also be executed when "name" is referred to as a simple command run. You can specify a redirect when the function execution is rejected. The exit state of a function is 0 unless it is a syntax error or if a read-only function with the same name already exists. When the function is executed, the exit state of the function is the exit state of the last executed command in the function body.

(To be Continued ...)

This article is from the "home of the Ops" blog, so be sure to keep this source http://zhaotianyu.blog.51cto.com/132212/1785619

Basic analysis of Linux operating system (vii)--bash (Shell) Basics (2)

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.