Prerequisites: The shell environment pdksh and Bash; the default shell environment in Ubuntu is Bash.
Syntax:
<1> Variables
1. There are three types of variables: environment variables, internal variables, and user variables. The environment variables and internal variables can be directly used without user-defined requirements.
Environment variable: defined by the system. You do not need to define but can change the value of the variable.
Internal variables: a special type of variables provided by the system. This variable is used in the program to make a decision. These variables cannot be modified in Shell programs.
Some internal variables are as follows:
$ # Number of location parameters passed to the shell program;
$? The final command completes the Code or the shell program executed in the shell program;
$0 shell program name;
$ * The shell program is called to send a single variable string.
User variables: User-Defined variables that can be modified by users.
Note: 1. variables are non-typed (if any );
2. No need to declare
3. Assignment Example A = 10; "=" cannot have spaces on either side
There are three types of string assignments: "=" and then directly followed by the string; the string is enclosed by single quotation marks; the string is enclosed by double quotation marks. But there are differences.
1. When a string contains spaces, the first or third type cannot be used;
2. Add ''to prevent shell from parsing variables.
3. Add "" to parse all variables in the string
For example, an error occurs when x = ABC def. The correct format is X = 'abc def 'or X = "ABC Def"
Double quotation marks
Var = "test string"
Newvar = "value of VaR is $ Var"
Echo $ newvar
Output result: Value of VaR is test string
Single quotes
Var = "test string"
Newvar = 'value of VaR is $ var'
Echo $ newvar
The output result is: Value of VaR is $ var.
Backslash
A backslash can be used before a character to prevent shell from interpreting subsequent characters as special characters.
Test = 3
Var = $ Test
Echo $ VaR
Output result: 3
Test = 3
Var =/$ Test
Echo $ VaR
The output result is: test.
<2> Expression comparison
The operators that execute logical comparison in different shells are different. In pdksh and bash, a command named test can be used to compare expressions. The test command supports the following four types of comparison:
1. String comparison
= Compare whether two characters are equal
! = Compare whether two strings are not equal
-N: whether the length of a string is greater than zero
-Z: whether the string length is equal to zero
2. digit comparison
-EQ: Compares two numbers for Equality
-Ge: determines whether the first number is greater than or equal to the second number.
-Le: determines whether the first number is less than or equal to the second number.
-Ne: determines whether two numbers are different.
-GT: compare whether the first number is greater than the second number.
-Lt: compare whether the first number is smaller than the second number
3. File Operation comparison
-D: determines whether the file is a directory.
-F: determines whether a file is a common file.
-R: determines whether the read permission is set for the object.
-S: determines whether the length of the file name is greater than 0
-W: Determine whether the file has write permission
-X indicates whether the file has the execution permission.
4. Comparison of logical operators
! Non-logical
-O logic or
-A logic and