#/Bin/bash
1
, Wildcard
*
: Match any character or string, including an empty string.
? : Match any character. For example :?
ABC
Can match any
ABC
It is a string of four characters that can be switched to or from any character.
[…]
: Match any single character listed in brackets. For example
Abd [DEF]
Match
ABC
Starting
Def
Any character string ending with one character.
2
, Alias, custom a command, as the abbreviation of other commands, reduce keyboard input. For example:
Alias List = 'ls
-L'
Cancel alias:
Unalias list
3
, Add executable permissions to the script:
Chmod + X file. Sh
4
,
Shell
The variables are divided into three types: local variables, environment variables, and location variables.
Shell
A language is an interpreted language. A variable is defined when a variable is assigned a value.
Program variables have no data types. You can use the same variable to store values of different data types.
Add a variable
$
The value of the access variable.
Value assignment:
=
A string without spaces can be assigned a value without quotation marks.
5
, Environment variable, is a variable that can be accessed by a user in all processes.
Use
Export
Command to view the list of system environment variables.
To access environment variables, you must add
$
Symbol.
Define an environment variable:
Export xx= 1234
Echo $ XX
6
, Location variable, yes
Shell
Parameters that are passed in when the program is running. The program calls these parameters as variables and stores them
1 ~ 9
Total
9
Is visually called a location variable. For example
#! /Bin/bash
# Test. Sh
Echo $1
Echo $2
Run:
./Test. Sh Beijing
ABCD
The result is as follows:
Beijing
ABCD
7
Arithmetic Operators:
+
,
-
,
*
,
/
,
**
,
%
,
+ =
,
-=
,
* =
,
/=
,
% =
8
, Use
Expr
Change the operation sequence. For example
Echo 'expr 1 + 2'
, Result:
1 + 2, Echo 'expr 1 + 2', result: 3
Use
Let
Indicates mathematical operations.
B = Let 1 + 2
And then use
Echo $ B
Output
B
Value
3
$ []
Indicates a mathematical operation. The content in the brackets first performs mathematical operations. For example
Echo $[1 + 2]
9
,
Echo
Command can output the value of a file or variable, which is
Shell
The most common output method. It can be output to a terminal or file.
Echo $ Str
#
Output to Terminal
Echo $ Str
> File
#
Output to file
File
If no file exists, a new one is created. If it already exists, it overwrites the previous file. If it is appended to the file, use
>
.
Format controller:
/C
Does not wrap the output;
/N
, Output a line feed;
/T
Output a hop cell;
Echo
Houjia
-E
Option to output special characters. Add
-N
Can be disabled
Echo
Line feed after output.
10
,
Read
Command to read information
Read
The command can read information from the keyboard or file and assign it to a variable.
If a variable is read-only, all input information on the keyboard is assigned to the variable,
Enter
The key ends. If multiple variables are read, use the space key to separate the input variables. If the number of input variables is greater than the number of variables to be read, the remaining variables are assigned to the last variable. Add
<FILENAME
To read data from the file and assign it to the variable. For example
Read a B <a.txt
11
, File redirection:
CMD>
Filename:
Redirects the standard output to a file.
CMD>
Filename:
Redirects the standard output to a file in append mode.
CMD 1>
Filename:
Redirects the standard output to a file.
CMD
> Filename 2> & 1:
Redirects standard output and standard errors to a file.
CMD 2>
Filename:
Directs the standard output errors to a file.
CMD
> Filename 2> & 1
Append the standard output and standard error to a file.
CMD <
Filename1> filename2:
Set
CMD
Command
Filename
File as the standard input,
Filename2
File as standard output
CMD <
Filename
:
CMD
Command
Filename
File as standard input
For example
A.txt
Convert all lowercase letters in to uppercase letters, and save them to the file.
B .txt
Medium
Tr "[A-Z]" "[A-Z]"
<A.txt> B .txt
12
Double quotation marks (") indicate a string. Strings cannot be used directly.
$
Special characters such as quotation marks, double quotation marks, backslash, and quotation marks. If there is no space in the string, double quotation marks are used to cause the string with a value assignment, which is the same effect as not using double quotation marks. When a string contains spaces, double quotation marks are used to indicate the content in quotation marks as a string.
13
, Reverse quotation marks, reverse quotation marks
`
Not single quotes,
"Tab"
The symbol on the key. The reverse quotation marks are used to execute the system command in the quotation marks and then return the result of the command.
Echo 'date'
A = 'LS'
Echo $
14
, Backslash, used to escape special characters.
STR = // %/*/&; echo $ Str
15
, File status test
Test-x a.txt
#
Test files in the current directory
A.txt
Executable?
-D
Test whether the file is a directory file
-S
Not empty
-F
Whether it is a regular file
-W
Writable or not
-L
Is it a symbolic connection?
-X
Executable or not
-R
Readable or not
-U
Yes
SUID
Bit settings
16
, Numerical testing, refers to comparing two values of the size or equal relationship.
Test
Command:
Test
First operand
Value Comparison Operator
Second operand
Use brackets instead
Test
Command:
[
First operand
Value Comparison Operator
Second operand
]
Value Comparison OPERATOR:
-EQ:
Equal
-Ne
: Not equal
-Le
: Less than or equal
-Ge
: Greater than or equal
-GT
: Greater
-Lt
: Less
Example:
Test 3-EQ 5;
Equivalent
[3-EQ 5]
17
, String Tester:
=
: Whether the two strings are equal
! =
: Not equal
-Z
: Whether it is a Null String
-N
: Non-empty string
18
, Logical Tester:
-
Logic and;
-O
Logic or;
!
Logic No.
[
-W a.txt-a-x a.txt]; echo $?
19
Process Control Structure
If
Statement:
If
Condition
Then cmd1
Else cmd2
Fi
If
Condition
1
Then cmd1
Elif
Condition
2
Then cmd2
Else cmd3
Fi
For
Statement:
For
Variable name
In
List
Do
Cmd1
Cmd2...
Done
Example:
#! /Bin/bash
#9*9
For I in 1 2 3 4 5
Do
For J in 1 2 3
4 5
Do
If [$ J-Le $ I]
Then
Echo-E "$ J/C"
Echo-E "*/C"
Echo-E "$ I/c"
Echo-E "=/C"
Echo-E "$ [$ I * $ J]
/C"
Fi
Done
Echo ""
Done
Until
Statement:
Until
Condition
Do
Cmd1
Cmd2...
Done
Example:
#! /Bin/bash
#1 + 2 +... + 100
Sum = 0
I = 1
Until [$ I-GT 100]
Do
Sum = $ [$ sum + $ I]
I = $ [$ I + 1]
Done
Echo $ sum
20. execute commands in the background
If an & symbol is added to the end of the command line, the shell will immediately return to the prompt and execute the command in parallel in the background. You can start the command without waiting. The output result of the background job during execution will be sent to the screen.
! The variable contains the pid of the last job submitted to the background. You can use kill-9 $! Kill the background process.
The jobs command displays the jobs currently running in the background.
FG % num can send jobs specified by num numbers to the foreground for execution. If no number is specified, the recently executed background job is sent to the foreground for execution.
#! /Bin/bash <br/> str1 = "badasealya" <br/> str2 = "badasealy" <br/> If ["$ str1"/> "$ str2"] <br/> then <br/> echo "yes" <br/> else <br/> echo "no" <br/> fi <br/> echo "end! "</P> <p> #! /Bin/bash <br/> str1 = "badasealy" <br/> str2 = "badasealy" <br/> If ["$ str1" = "$ str2"] <br/> then <br/> echo "yes" <br/> else <br/> echo "no" <br/> fi <br/> echo "end! "<Br/>