Specify the script interpreter
#!/bin/bash
Note: 1, the declaration can only be placed in the first line of the script, indicating that the script uses the Bash interpreter to interpret execution, otherwise it will be interpreted as Comment 2, bash must use absolute path
3, the declaration is only in the script as an executable program, through the./test.sh call is only valid, if through the SH test.sh will directly run the SH interpreter test.sh just as a parameter pass
Debug Scripts debug scripts with parameter-X
Sh-x test.sh
This executes the script and displays the values of all the variables.
The shell also has a pattern that does not need to execute a script just to check the syntax. You can use this:
Sh-n Test,sh
This will return all syntax errors
Shell Variables
1, ReadOnly set read-only variable
#!/bin/bash
name= "Jack"
readonly name
name= "Jams"
ReadOnly decorated, the value of name can no longer be changed, otherwise the execution will error: Name:readonly variable
2, unset delete variable is equivalent to set the variable to NULL, unset can not delete read-only variable
3. Variable type when you run the shell, there are three variables:
1 local variable local variables are defined in scripts or commands, only valid in the current shell instance, and other shell-initiated programs cannot access local variables.
2 environment variables All programs, including shell-initiated programs, can access environment variables, some programs need environment variables to ensure their normal operation. When necessary, shell scripts can also define environment variables.
3 Shell variable Shell variable is a special variable set by the shell program. Some of the shell variables are environment variables, some of which are local variables that guarantee the shell's normal operation.
4, special variable $ The filename of the current script
$n arguments passed to a script or function. N is a number that represents the first few parameters. For example, the first argument is $, and the second argument is $.
$# the number of arguments passed to a script or function.
$* all parameters passed to a script or function.
$@ all parameters passed to a script or function.
$? The exit state of the previous command, or the return value of the function.
$$ the current shell process ID. For Shell scripts, the process ID where these scripts are located.
Both $* and $@ represent all parameters passed to a function or script and are not included in double quotes ("") to "$" ... Output all parameters in the form of "$n".
But when they are included in double quotes (""),
"$*" will all parameters as a whole, in the form of "$ ... $n" output all parameters;
"$@" will separate the parameters to "$" "$" ... Output all parameters in the form of "$n".
single and double quotesSingle quotes are used to keep the literal values of all characters in quotation marks, even if the and carriage returns within quotation marks are no exception, but single quotes cannot be found in strings.
Double quotes are used to keep the literal value of all characters in quotation marks (carriage returns are no exception), except in the following cases:
$ plus variable name can take the value of a variable
The inverted quotation mark still represents the command substitution
\$ represents the literal value of $
\ ' Representations ' of the literal value
\ "Representations" of the literal value
The literal value of \ \ =
In addition to the above, there is no special meaning in front of other characters, which only represents the literal value.
Arithmetic operations
The following table lists the commonly used arithmetic operators, assuming variable A is 10 and variable B is 20:
operator |
Description |
examples |
+ |
Addition |
The result of ' expr $a + $b ' was 30. |
- |
Subtraction |
' Expr $a-$b ' result is-10. |
* |
Multiplication |
The result of ' expr $a \* $b ' was 200. |
/ |
Division |
The result of ' expr $b/$a ' was 2. |
% |
Take more |
' Expr $b% $a ' results are 0. |
= |
assigning values |
The a= $b assigns the value of variable B to a. |
There are spaces between expressions and operators, such as 2+2, which must be written 2 + 2, which is not the same as most programming languages that we are familiar with.
The complete expression is "contained", noting that this character is not a common single quote, under the ESC key.
Multiplication symbols need to be escaped
In addition to expr, you can also use (()) to represent, for example: var=$ ((20 + 5))
relational operators for numbers
Relational operators support only numbers, and strings are not supported unless the value of a string is a number.
The following table lists the commonly used relational operators, assuming variable A is 10 and variable B is 20:
operator |
Description |
examples |
-eq |
Detects whether two numbers are equal, and equality returns TRUE. |
[$a-eq $b] returns false. |
-ne |
Detects whether two numbers are equal, not equal returns TRUE. |
[$a-ne $b] returns TRUE. |
-gt |
Detects whether the number on the left is greater than the right and, if so, returns True. |
[$a-gt $b] returns false. |
-lt |
Detects whether the number on the left is less than the right and, if so, returns True. |
[$a-lt $b] returns TRUE. |
-ge |
Detects whether the left number is greater than or equal to the right, and returns true if it is. |
[$a-ge $b] returns false. |
-le |
Detects whether the left number is less than or equal to the right, and returns true if it is. |
[$a-le $b] returns TRUE. |
Boolean/Logical Operations
The following table lists the commonly used Boolean operators, assuming variable A is 10 and variable B is 20:
operator |
Description |
examples |
! |
is not an operation, returns false if the expression is true, otherwise returns true. |
[! false] returns TRUE. |
-O |
Or operation, and returns True if one of the expressions is true. |
[$a-lt 20-o $b-GT 100] returns TRUE. |
-A |
And operation, all two expressions are true to return true. |
[$a-lt 20-a $b-GT 100] returns FALSE. |
The following describes the logical operators of the Shell (note with double brackets), assuming variable A is 10 and variable B is 20:
operator |
Description |
examples |
&& |
Logical AND |
[[$a-lt && $b-GT 100]] returns false |
|| |
Logical OR |
[[$a-lt | | $b-GT 100]] returns True |
string Operators
The following table lists the commonly used string operators, assuming variable A is "ABC" and Variable B is "EFG":
operator |
Description |
examples |
= |
Detects whether two strings are equal, and equality returns TRUE. |
[$a = $b] returns FALSE. |
!= |
Detects whether two strings are equal, not equal returns TRUE. |
[$a!= $b] returns TRUE. |
-Z |
Detects whether the string length is 0, and returns true for 0. |
[-Z $a] returns false. |
-N |
Detects whether the string length is 0, not 0 returns TRUE. |
[-N $a] returns true. |
Str |
Detects whether the string is null and does not return true for null. |
[$a] returns TRUE. |
The > < of the string comparison is to be escaped, otherwise it will be treated as a redirection symbol
String Handling
Take length
Str= "ABCD"
expr length $str # 4
echo ${#str} # 4
Find the location of a substring
Str= "abc"
Expr index $str "a" # 1
expr index $str "b" # 2
expr index $str "x" # 0
Expr Index $ Str "" # 0
To intercept a child string
Str= "abcdef"
expr substr "$str" 1 3 # starting from the first position 3 characters, ABC
expr substr "$str" 2 5 # starting from the second position 5 characters, bcdef
expr substr "$str" 4 5 # starting from fourth position 5 characters, Def
Echo ${str:2} # Extract string from second position, Bcdef
Echo ${str:2:3} # from The second position begins extracting 3 characters, BCD
echo ${str: ( -6): 5} # Extracts the string from the penultimate position to the left, ABCDE
echo ${str: ( -4): 3} # Extract 6 characters to the left from the penultimate position, CDE
str= "ABBC,DEF,GHI,ABCJKL"
Echo ${str#a*c} # output, DEF,GHI,ABCJKL a well number (#) Indicates that the shortest match is intercepted from the left (here the ABBC string is removed)
echo ${str# #a *c} # output JKL, two number (# #) indicates the longest match from the left (here's Abbc,def,ghi, ABC string remove)
echo ${str# "A*c"} # output ABBC,DEF,GHI,ABCJKL because there is no "a*c" substring
echo ${str## "A*c"} # output ABBC in str, DEF,GHI,ABCJKL Empathy
Echo ${str#*a*c*} # empty
Echo ${str##*a*c*} # empty
echo ${str#d*f) # output ABBC , DEF,GHI,ABCJKL,
Echo ${str#*d*f} # output, Ghi,abcjkl
echo ${str%a*l} # Abbc,def,ghi a percent semicolon (%) Represents the shortest match from the right
Echo ${str%%b*l} # A two percent sign (%) representing the longest matching
echo ${str%a*c} # Abbc,def,ghi from the right , Abcjkl
String substitution
Str= "Apple, tree, apple tree"
Echo ${str/apple/apple} # replaces the first occurrence of Apple
Echo ${str//apple/apple} # Replace all Apple
echo ${str/#apple/apple} # if string str starts with Apple, replace it with Apple
Echo ${str/%apple/apple} # If string str ends with Apple, replace it with Apple
Case conversion
# #转为大写
echo $PATH | tr ' [A-z] ' [A-z] '
# #转为小写
echo $PATH | tr ' [A-z] ' [A-z] '
File Test Operators
File test operators are used to detect various properties of Unix files.
Attribute detection is described as follows:
operator |
Description |
examples |
-B File |
Detects if the file is a block device file, and returns True if it is. |
[-B $file] returns FALSE. |
-C file |
Detects if the file is a character device file, and returns True if it is. |
[-C $file] returns false. |
-D File |
Detects if the file is a directory, and returns True if it is. |
[-D $file] returns false. |
-F File |
Detects whether the file is a normal file (neither a directory nor a device file), and returns True if it is. |
[-F $file] returns TRUE. |
-G file |
Detects whether the file has a SGID bit set and, if so, returns True. |
[G $file] returns false. |
-K File |
Detects if the file has a sticky bit (Sticky bit), and returns True if it is. |
[-K $file] returns false. |
-P File |
Detects if the file is a well-known pipe, and returns True if it is. |
[-P $file] returns false. |
-U file |
Detects whether the file has a SUID bit set and, if so, returns True. |
[-U $file] returns false. |
-R File |
Detects if the file is readable and, if so, returns True. </ |