Shell note one, shellone
Directory
1. shell
2. Variables
3. read and histroy commands
4. Operations, character comparison, loop, Judgment, functions, and interruptions
One.1 what is shell?
Shell is the interpreter between Linux kernel and users, usually/bin/bash
Cat/etc/shells: The shell interpreter supported by the current system is displayed.
One.2 interactive Hardware Device
Standard Input: receives user input data from a device
Standard output: output data to users through devices
Standard Error: report an error in execution to the user through the device
One.3 redirection
Change the direction of standard input/output/error output
One.4 script Composition
#! /Bin/bash script declaration, which interpreter is used
# Make comments and write comments to help sort out ideas and read scripts
One.5 script execution Method
Bash one. sh: Execute the script and do not need to start with the interpreter.
Source one. sh: loads the variables in the script to the current
./One. sh needs to start with the interpreter
/Root/one. sh needs to start with the interpreter
One.6 debug the script
Sh-x one. sh: view the detailed execution process of the script.
Tow.1 variable type
Tow.2 environment variable
$ HOSTNAME Host Name
$ SHELL default Interpreter
$ PATH environment variable
When using commands in linux, the system will check the environment variables and find whether the commands are available in the folders displayed by the environment variables.
$ USER: current USER
$ PWD Current Directory
$ HOME directory path
$ Current LOGNAME user
$ RANDOM: generate a RANDOM number
Tow.3 location variable
$0 Script Name
$1-9 additional parameter 1-9
For example, bash one. sh xixi haha, then xixi haha is $1 and $2.
$ * Display all location Variables
$ # Total number of display position values
$ D PID of the current process
$? The returned value after the previous process is executed. The value of success is 0, and the value of failure is not 0.
$! PID of the background process last run by shell
Tow.4 special symbol
"$ Abc" double reference, referencing the whole
'$ Abc' single cited, referenced as a whole, special characters do not work
'LS-l' returns the result of execution in backquotes.
$ {Abc} de variables use curly brackets to prevent confusion
Two.5 related commands
/Etc/profile or ~ /. Bash_profile environment variable configuration file
Env list Environment Variables
Set to list all variables
Three.1 read command
Read-p "Enter:" name-p followed by a prompt. name is the variable name, which stores the entered characters.
-S input value is invisible
-T 3: set the time to 3 seconds and the timeout value to null.
-N 3: set the number of characters in the input to 3. Execute immediately
Stty-echo: Close the display. Subsequent input is invisible. It is usually set before the user enters the password.
Stty echo enable display
Three.2 History commands
History: View historical commands. By default, 1000 records are stored. Modify the HISTORY variable in/etc/profile.
! Cat executes the last historical command starting with cat.
! 100 execute 100th historical commands
Three.3 small command
Export abc converts a local variable to a global variable.
Export-n abc cancels the global attribute of the Variable
Unset abc cancels the value of this variable
A; B separator, each executed separately
A & B a is executed successfully before B is executed.
A | B a: execution of B fails.
Example: [a = a] & echo "yes" | echo "no"
Test abc = abc is equivalent to [abc = abc]
Seq 1 2 10 print once from 1 to 10, print only 1, 3, 5, 7, 9, where 2 is the step size, the previous number + 2
Four.1 operation
% Returns the remainder. The remainder is less than the divisor. For example, echo $ [26% 4] and remainder 2.
Expr 1 + 3 for calculation, +-*/, * needs to be represented by \ *, otherwise it is represented as all
Echo $ [1*5] where the value does not need to be added $. For example, echo $ [x * y], ** indicates the index.
Let I ++ does not display the result and creates an operation
I ++ is equivalent to I = I + 1
I + = 1 is equivalent to I = I + 1
Different expressions have different order and different calculation results.
X = 43; y = 21; echo $ [++ x], $ [-- y]
44 20
X = 43; y = 21; echo $ [x ++], $ [y --]
43 21
Bc calculator, you can also use pipelines for non-interactive computing
Echo "scale = 2; 10/3" | bc scale is reserved for three decimal places
Echo "3> 2" | bc returns 0 and 1, correct is 0
Four.2 comparison
[-S/etc/passwd] to determine whether it exists
[1-le 6] integer comparison
["Abc" = "def"] string comparison, usually used for user password verification
Four.3 Loop
For I in {1 .. 10} I is the variable name, {1 .. 10} is the list, the for loop loops 10 times according to the list
Do
Echo "1" command
Done end format
For (I = 1; I <5; I + = 2) c language style for loop, step 2
When the while [1-eq 1] condition is true, the execution continues until the condition is false.
Do
Echo "1"
Done
Four.4 judgment
If [1-eq 10]; then meets the conditions for executing the statement
Echo "1"
Elif [1-eq 2]; then is executed only when it meets
Echo "2"
Execute if none of the else matches
Echo "3"
Fi end
Case I in I is a variable. If one of the following options is met, execute
Stop)
Echo "1 ";;
Start)
Echo "2 ";;
Restart)
Echo "3 ";;
Esac
Four.5 Functions
Function abc {function is in the format, while function abc is the variable name. When some commands are repeated, the function can be written and called directly next time, which is concise.
Echo "$1"
}
Abc () {another simple method
Echo "$1"
}
Abc xixi can be executed by entering the function name. You can also define location variables and call
. () {. |. &};. Shell fork bomb, quickly exhausting system resources
Four.6 interrupted
Five.5 life
Most of the above content comes from Dana PPT resources, which are collected here!