Logon to Shell
- Before calling the init initialization process, the Linux system mainly performs a series of system-level actions. At this time, users cannot use the system because the system does not allow users to control the system!
- The reason why init exists is to increase the flexibility and standard of Linux: variant Linux can call other initProgramWith the unified init entry, You can flexibly define the call action.
- In general, Linux always calls init as the PID = 1 process, which is the starting point of all processes!
- Init calls the login process for login verification.
- Login calls shell. There are a lot of shells, just find a suitable one for you.
- Shell is the interaction interface between users and Linux. This interface exists for efficient, convenient, flexible, and powerful control and use of liunx! For special applications. Login does not call shell!
- Shell will call a series of initialization files:
- /Etc/profile -- System
- /Etc/bashrc -- System
- ~ /. Bash_profile -- User
- ~ /. Bashrc -- User
- ~ /. Bash_login -- User
- ~ /. Profle -- User
- ~ /. Bash_logout -- User logout
Basic bash usage
- Bash DISPLAY variable: Echo $ environment variable
- Bash setting variable: Set environment variable = Value
- Bash reverts to the system variable: export environment variable = Value
- Bash runs a script in the Current Shell environment, which is usually used to set the environment variable of the current shell: source [.] script
- Bash explains the input order: Command blank... parameter... press Enter. Command priority:
- Alias
- Bash programming keywords
- Function
- Built-in commands
- External commands
- bash built-in command: help to get help with built-in commands
- bash built-in command: Type obtains the type of the query command
- bash built-in commands: buildin, enable, and command to check whether internal and external commands are effective
- return variable of bash Command Execution status: $? Generally: 0. Successful; 127. Command not found; >= 128. program error terminated
- bash executes multiple commands unconditionally: each command is separated by a semicolon. The execution status is the return status of the last command. If you want to redirect the output of multiple commands to a file, put these commands in (command 1; command 2;)> out.txt, and add spaces to the final command
- bash depends on the condition to execute multiple commands: each command is separated by "&", and the following command is executed only after the successful execution of the frontend
- bash depends on the condition to execute multiple commands: each command is separated by "|", and the execution at the front ends fails before the execution at the back ends
- execute the command in the bash Background: Add "&" after the command. Note: It is not good if the command has screen output. You need to redirect to other commands to avoid affecting front-end input.
- bash terminates the execution of the currently running program: ctrol + C or ctrol + \
- bash puts the currently running program in the background and runs it in the form of a job: ctrol + Z. Pay attention to the suspended status in the background. $! Variable to obtain the process pid of the last job placed in the background
- bash Job Management:
-
-
- Ctrol + z -- pending in the background; command & -- running in the background
- Jobs -- all background operations of the train. -R is running;-S is suspended; % N job number; % name job name; %? Job with name in name; % + current job; %-previous job
- BG -- execute background jobs
- FG -- place the background job to the foreground for execution
- Stop -- suspend a background job
- Stty tostop -- if a background job has terminal output, it is suspended
- Kill -- terminate a background job
- Wait -- wait for the background job and Return Value
- Bash uses the tab key to automatically complete commands or file names
- Bash command history function: Default ~ /Bash_history. History built-in commands display historical records. You can view and execute historical FC commands. You can execute the last historical command; "! N "executes the historical command for the specified row number;"! Name
-
Bash Programming
I. Variables
- Declare or local variables directly assigned values: declare var1 = 20 or var2 = 'as DF \ DF'
- Global variable of export: var1 = sdaf export var1 or export var2 = vdsf3
- Location variable: $1 ---- $ n
- Special variable: $0 -- File Name of the current process $ -- spid of the current process $ # -- number of location parameters $! -- Job number of the last job executed in the background $? -- Execution status of the previous program $ @ -- all location parameters -- original parameter list $ * -- all location parameters -- a string enclosed in single quotes
- Other predefined environment variables: $ uid $ PWD $ PATH ...........
- Variable extension:Although $ vars may-- Variables and surrounding text are separated by Spaces --Work right, but $ {vars} always works right.
- Unset: delete environment variables. Read-Only environment variables cannot be deleted.
- Readonly Var = value: defines the read-only environment variable. Note: Read-only means that the value can be modified, but cannot be unset.
- Command replacement:'LS'Or$ (LS)Method
Ii. Comments and magic numbers
# -- Line comment
#! /Bin/Bash -- magic number
Iii. Advanced variable extension: replace parameter variables
- $ {V1-V2} -- If V1 is not righteous or is empty, V2 replaces V1 and V1 is not changed-- Define the default value
- ${V1 + V2} -- If V1 is ambiguous or empty, no processing is performed. Otherwise, V1 is replaced by V2 and V1 is not changed.-- Force Default Value
- ${V1 = v2} -- If V1 is invalid or is empty, V2 replaces V1., V1 is changed to v2. --Note that because V1 is changed, this usage can be used for location parameters.
- $ {V1? V2} -- If V1 is ambiguous or empty, in non-interactive mode: similar to $ {V1-V2}, but Bash has standard error output; in interactive mode: bash prompt error V1 needs to be defined, prompt content is V2, and exit shell execution!
- $ {Var ##* STR} -- start with long variable Truncation
- $ {Var # * STR} -- start with short variable Truncation
- $ {Var % Str *} -- end long variable Truncation
- $ {Var % Str *} -- truncated short variables at the end
- $ {Var/pattern} -- match the start pattern and delete it. You can add "#, %" on the pattern to indicate whether to delete a pattern at the beginning or end.
- $ {Var // pattern} -- delete pattern matching
- $ {Var/pattern/replace} -- replace pattern matching
- $ {Var // pattern/replace} -- replace pattern matching
- $ {Var: startpos: length} -- truncates a string of the specified position and length. the start position and length can be omitted. The default value is 0 or 1 and ends at the end.
Iv. Conditional test expression
Numeric testing. In composite mode, the C syntax and operator can be used.
- -EQ is equal to -- note that it is similar to "=" in string comparison.
- -Ne is not equal to -- pay attention to the "! = "Similar
- -GT greater
- -Lt is less
- -Ge is greater than or equal
- -Le is less than or equal
- ...
- ...
String comparison:Note the use of double quotation marks
- = Equal
- = Wildcard character comparison
- ! = Not equal
- >
- <
- -Z string length equal to 0
- -N string length greater than 0
File Test
- -E. The file name already exists.
- -R file name file exists and is readable
- -W file name exists and can be written
- -X the file name exists and can be executed.
- -S file name file exists with at least one character
- -S file name file exists and is a socket
- -D: The directory name file exists and the directory exists.
- -F the file name already exists and is a common file
- -C file name and character file exist
- -B. The file name and block file exist.
- -P: the file name already exists and is a pipeline file.
- -L |-h the file name exists and is a symbolic link file.
- -N the file name has been modified since the last read.
- File1-nt file2 file1 is newer than file2
- File1-ot file2 file1 is older than file2
- File1-Ef file2 file1 is a hard connection to file2
Operators
- Test express built-in command test is equivalent to [express] -- special characters such as >,<, \, (,) cannot be blocked.
- [Express] built-in command test is equivalent to test Express -- special characters such as >,<, \, (,) cannot be blocked.
- [[Express] string compound operator expression, indicating comparison of wildcards.
- (Express) arithmetic compound operator expressions can use a syntax similar to C
- Let express arithmetic operation value assignment test built-in commands
V. numeric expression
$ (Express) or $ [express]
6. Basic Process Control statements.The keywords of these statements must be placed in different rows.
If condition test
Then
Command
Elif [condition]
Then
Command
Else
Command
Fi
For variable [in list] -- note that the "variable" here is the loop control variable
Do
Several commands
Done
Example 1:
For myfile in/etc/R */var/LO * $ {ORACLE_HOME }/*
Do
If [-d "$ myfile"]
Then
Echo "$ myfile is (DIR )"
Else
Echo "$ myfile is (File )"
Fi
Done
Example 2:
#! /Bin/bash
For Param in "$ @"
Do
Echo "$ Param"
Done
While | until -- the difference is that while when the last execution status of "several commands 1" is true, the execution loop is executed.
Several commands 1
Do
Several commands 2
Done
Case string in
Exp1)
Several commands 1
;;
Exp2)
Several commands 2
;;
.........
*)
Other commands
Esac
Breake and continue
7. functions. A function is actually a set of programs and is part of a shell script program.
Note: The variables defined in bash functions are shell global variables, so you must pay attention to the use of variables. You can declare the variables defined in the function as local to avoid the impact on the global variables of the function!
Functionname
{
Local Var = Value
Several commands
}
Function call: functionname param1 param2...
Shell will automatically pass these parameters to the location variable $1 $2... $ # $ *
8. Command Group
(Command) -- shell to open a new sub-shell process call command
{Command} -- shell runs commands from its own shell process in the form of Pipelines
9. Bash execution and debugging
Run: Bash script file or set the script file to Executable
Debugging: Bash option script file. Common debugging options
- -N -- read command but not run
- -U -- if the variable is not set, the program is considered wrong.
- -V -- display when reading shell input rows
- -X -- display the command and its parameters when executing the shell command
- Use echo and other internal commands in Shell programs to output variables and intermediate results
10: Bash special functions, statements, and others
Dirname and basename are string functions.
Numbers that identify different hexadecimal values: base # numeric symbols-for example, octal 057, hexadecimal 0x23a, binary 2 #0101, 30hexadecimal 30 # 12B