recall that we talked about user management, rights management, and also introduced two commands, one is the install command, the other is mktemp, here we do not do more introduction.
For user management, it is the allocation of the resources of the permission to their isolation.
For permissions, it is the permissions that the logged-on user of the system should have assigned to the resource.
So we'll cover the bash feature and the initial programming of the Bash script in this section.
First, what is bash?
Let's take a look at a picture to explain what bash is.
in this picture we can see that the most internal is the hardware, and then the kernel, the kernel will be the complexity of the hardware and the difference between the hidden, Output into a unified and concise interface, we call it a system call, in which the programmer uses this interface can ignore the complexity of the underlying hardware, the programmer through the system call interface to develop the program, but the system calls in order to do enough concise, so it does the very bottom, Just to hide the complex hardware, and then outward output, because the system calls the interface is too low, so the development cycle of the program is very long, and maintenance is very inconvenient, but in the early indeed there is a great advantage, because in exchange for better performance, but now the computer performance has been greatly improved, In order to make the program development faster, even if there is write redundant code does not matter, so in the system call has encapsulated a library function, so that the development program can directly call the library function can be, away from the person and a step further.
Terminal is the means of user interaction, is the first thing our users contact, we use the terminal to control the core indirectly, the user is not able to operate the kernel, because in this link chain, the weakest link is human, And the interface of the terminal we also call the Shell interface, in the third chapter we described the Shell interface, respectively, GUI and Cli,gui in the server is not commonly used, and the CLI interface is a text interface, the server must use it to operate the system, How we view the system support those shells are viewed in/etc/shells.
# Cat/etc/shells/bin/sh/bin/bash/sbin/nologin/usr/bin/sh/usr/bin/bash/usr/sbin/nologin
Second, bash features
In addition to the command completion and command line expansion we've talked about in bash, there are other features that we'll now enumerate about bash features:
Command line expansion: ~, {} command alias: Alias, Unalias Command history: Historical file name wildcard: GLOB command environment variable: $PATH Shortcut: Ctrl + A, E, U, l path Completion:
Let's take a look at the alias, the alias is to hide the original command, replaced by a user-man command, it is a shell internal command, the command format is as follows:
alias [-P] [Name[=value] ...]
However, set in the shell, an exit terminal is not, so we can write in the configuration file, for example:. BASHRC can take effect after exiting the terminal, but this is a personal, not global setting.
2.1 Bash feature command hash
We have previously said that the command in the $PATH environment variable to find the process, we enter a character in the shell, the shell will be from the $path from left to right to find the command, if found to stop finding, can not find the shell will also prompt the corresponding result, hash command is the $ The command found in the path variable is cached and the result of the command is placed in memory so that the cached command does not have to be found in the $path variable. Its storage format is: Key-value.
Key: Search Key value: Value
The options for the hash command are:
Hash: to list; hash-d command: Delete a command; Hash-r: empty;
2.2 Bash feature variables
Any programming language has variables, the program is composed of instructions plus data, instructions are provided by the program files, and the data provided by the IO, files, pipelines and variables to provide, so the program can be called the algorithm plus data structure.
The participation of a variable is to put some data of the file into memory for CPU instruction running or operation, the result is put into another memory, and finally output it.
A variable is also a way to provide data, which is a space for a memory run request, and the variable name is the starting address of the memory run space. So the variable name + points to the space together. Methods for assigning variables we've talked about Name=value before.
There are many types of variables, which refers to the storage type of the value, based on its storage format (mainly numeric or character), the data range represented, and the participating operations, mainly around these, the defined variable type defines its storage format and its ability to participate in the operation. And the type of programming language, according to the type of strict requirements, we are broadly divided into two categories, one is a strongly typed variable programming language, can not have a little violation of the rules, it is numeric value, first compiled and executed, the other is a weakly typed variable programming language, side interpretation side execution. The shell is a weak type of language, because bash sees all the variables as character types. And bash's variables don't have to be declared beforehand; it's equivalent to implementing both the Declaration and the assignment process. So the declaration of the usual variable is the type and the variable name.
We convert the place where the variable name appears to the data place called the variable substitution, in other words, the position where the variable name appears is replaced with the data in the memory it points to, and this is called the variable substitution.
Then the variable reference is the data in the memory space that the variable name points to. For example ${var_name}, $var _name.
The command for variable names is defined as: variable names can only contain numbers, letters, and underscores, but cannot begin with a number. and the name of the variable names we have to do as much as possible to see the name of the meaning, the naming mechanism follows a certain law, can not use the program's reserved words, such as: if, else, then, for and so on.
2.3 Bash Variable type
The type of bash here is the variable type of its scope, a total of three classes, namely local variables, environment variables and local variables, as well as positional parameter variables and special variables. Summarized as follows:
Local variables: scoped to current shell only; environment variable: scope is the current shell process and its child shell process; local variable: scope is only a fragment of code (typically used in a function context); positional parameter variable: passing arguments to the shell process executing the script; special variables: she ll built-in variables with special functions; For example: $? 0: Success 1-255: Failure
Let's take a look at how local variables are used, primarily to see how variables are assigned and referenced, and to view variables and undo variables.
Variable assignment: Name=value variable reference: ${name}, $name "": Variable name replaces its value; ': variable name is not replaced with its value; View variable: Set undo variable: unset name Note: This is a non-variable reference;
the use of environment variables is also basically the same as local variables, but the scope is larger than local variables.
variable Assignment: (1) export name=value (2) name=value export name (3) declare -x name=value (4) name=value declare -x name Note: Do not add $ to the variable before the export, because this is a processing mechanism, not an application. variable references:${name}, $name Note: Bash has many environment variables embedded (usually all uppercase characters) To define the work environment for bash PATH, HISTFILE, HISTFILESIZE, HISTCONTROL, SHELL, Home, uid, pwd, oldpwd et; view environment variables: export, declare -x, printenv, env
We can also define read-only variables, but the read-only variable is nothing more than a re-assignment and does not support revocation; the lifetime is the life cycle of the current shell process, indicating that it is terminated with the shell process.
(2) readonly name
2.4 Bash features multi-command execution
Multi-command execution is the execution of multiple commands in the same command line, if your command has no logical relationship, just run it sequentially, use it, separate it, and execute all the commands sequentially, regardless of whether they are executed correctly or incorrectly.
# COMMAND1; COMMAND2; COMMAND3; ...
There is a multi-command execution between logical operations, according to the Boolean type to get its results, is true or false.
True (True, yes, on, 1) False (false, no, off, 0)
However, for the state of the command to return the result, the true representation is 0, so the logical result of the operation: 0 means true. There are the following logical expressions of operation, respectively, with, or, not, then multiplication, divided by any number equals 0, is not an addition, 1 plus 0 equals 1, rather than the negation.
and: (multiplication) 1 && 1 = 1 1 && 0 = 0 0 && 1 = 0 0 && 0 = 0 or: (addition) 1 | | 1 = 1 1 | | 0 = 1 0 | | 1 = 1 0 | | 0 = 0 Non: ! 1 = 0 ! 0 = 1
So let's talk about the short-circuit rule, let's take a look at the case:
# COMMAND1 && COMMAND2
This means that if the COMMAND1 execution result is true, the COMMAND2 must be executed, and if the COMMAND1 execution state result is false, then COMMAND2 will not execute, which we call concatenation.
# COMMAND1 | | COMMAND2
This is when the COMMAND1 execution status result is true, the COMMAND2 will not be executed, but COMMAND2 will be executed if the COMMAND1 execution status result is false. This is what we call parallel.
Example:
ID $username | | Useradd $username
2017-11-27linux basic Knowledge (+) A preliminary script programming for Bash features