Directory
First, Shell Basic use of scripts
1. Language Specification
2. Variables
3. Redirect (>,>>)
Second, operators and common judgments
1. Comparison operators
2. Logical operators
3. Common Judgments
Third, the program structure
1. Branch (if statement)
2. Circulation
Iv. functions
The nature of the shell is an application that enables interaction between the user and the operating system and is an intermediary between the user and the operating system.
First, the basic use of shell scripts
1. Language Specification
(1) The file ends with. sh;
(2) The file header is: #! /bin/bash (indicates a bash shell is used);
(3) Start with # as a comment;
(4) Output: echo "Output content";
(5) Read: read-p "custom Read Prompt" name (name is the variable name to read the content setting, you can also set multiple variable names, separated by a space, separated by a space when reading the content and assigned to the corresponding variable name)
2. Variables
(1) Creation level assignment of variables
Variable name = variable Value
Variable names do not have to be declared types, consist of letters, numbers, and underscores, but cannot start with a number; The default uppercase variable name represents a system variable and lowercase denotes a normal variable;
Variable values can be characters, numbers, and commands, and when the value of a variable is a command, enclose the inverted single quotation mark (the key below the ESC);
Example: W=1 ' ls-l '
(2) Output of variable value ($)
You need to precede the variable with the $ symbol, when using echo plus single quotation marks when the output is only plain text, not the value of the variable is extracted, want to get the value of the variable, can only use double quotation marks to enclose the variable;
Show all user-defined variables with command: set
Show all system variables with the command: env
(3) Passing parameters to the program from outside the program
You can add $ $ inside a program to represent the first parameter that is passed from the outside of the program, and the same as the second argument, with a space separating the external arguments.
Example: to the program a.sh incoming ab,22 two parameters, using the command: a.sh AB 22
$-a represents the program itself, $ #表示所有输入参数的个数, [email protected] represents all parameters.
If it's not clear, refer to the sys.argv in Python, the mechanism is basically the same http://www.cnblogs.com/aland-1415/p/6613449.html
3. Redirect (>,>>)
The result is entered in a file by command
Example: Ls-l >name.txt
If the file does not exist, the file will be created, and if the file exists, the file will be overwritten with ">" and ">>" will be appended to the end of the file.
You can also add 1 or 2 in front of > or >>, note that there is no space between >,>>, where 1 indicates the correct command, and 2 is the wrong command.
Second, operators and common judgments
1. Comparison operators
equals: = =
Not equal to:! =
Greater than:> or-GT
Less than:< or-lt
Greater than or equal to: >= or-ge
Less than or equal: <= or-le
Only the > and other symbols can be used in the conditions in use (()), but not-gt, which is equivalent to some extent in the shell (()) and [].
2. Logical operators
Logic with:-A
Logical OR:-O
Logical non-:!
3. Common Judgments
-R filename: whether the file exists and is readable
-W file name: Whether the file exists and is writable
-X FileName: Whether the file exists and is executable
-F file Name: Determine if the file exists and is a normal file
-D file Name: whether the file exists and is a directory
-e File name: whether the file exists
-Z Variable: Determines whether the string length equals 0
-N Variable: string length is not equal to 0
In the conditional judgment that needs to be added [], if you add test in front of the-R and so on, do not add [];
Third, the program structure
1. Branch (if statement)
(1) Basic format:
if [condition]
Then
Statement 1
Else
Statement 2
Fi
Note: Spaces are required for each of the [] left and right ends of the condition statement, which enclose the variable in double quotation marks if it contains variables.
Then it can also be written after the condition, but add a semicolon after the condition.
(2) Multiple conditional connection formats:
if [condition1]; then
Sentence1
elif [Condition2]; then
Sentence2
elif [Condition3]; then
Sentence3
Else
Sentence4
Fi
(3) Case selection
Use format:
Case $ variable name in
"Value 1")
statement 1;;
"Value 2")
Statement 2;;
...
*)
Statement n
Esac
among them;; Represents the end of the statement, *) indicates that all results above are not in accordance with the last choice;
Values can also be used in intervals, but up to a maximum of 0-9 digits.
2. Circulation
(1) While loop
Format
while [condition]
Do
Statement
Done
Commonly used cyclic variables change the wording:
((i++))
((i+=1)
Let I=i+1
i=$ (($i + 1))
i=$ [$i +1]
Common Operations iterative notation:
((sum= $sum + $i))
sum=$[$sum + $i]
sum=$ (($sum + $i))
(2) For loop
For Iteration Child
Do
Statement
Done
The notation used by iterations of the For loop:
((i=1;i<=10;i++))
I in 1 2 3 4 5 6
I in ' SEQ 1 2 ' #1和15表示起始点, 2 = step
Iv. functions
function constructs
Function name () {
Content
}
A function call can be directly used with a functional name.
Linux Shell Basics