Special variables
Variable |
Meaning |
$ |
File name of the current script |
$# |
The number of arguments passed to the script or function. |
$* |
All parameters passed to the script or function. |
$? |
The exit state of the last command, or the return value of the function. |
$$ |
The current shell process ID. For Shell scripts, this is the process ID where the scripts are located. |
[Email protected] |
All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below. |
List of operator relational operators
Operator |
Description |
Example |
-eq |
Detects whether two numbers are equal and returns true for equality. |
[$a-eq $b] returns TRUE. |
-ne |
Detects whether two numbers are equal and returns true if they are not equal. |
[$a-ne $b] returns TRUE. |
-gt |
Detects if the number on the left is greater than the right and, if so, returns True. |
[$a-gt $b] returns false. |
-lt |
Detects if the number on the left is less than the right and, if so, returns True. |
[$a-lt $b] returns TRUE. |
-ge |
Detects if the number on the left is large equal to the right, and returns true if it is. |
[$a-ge $b] returns false. |
-le |
Detects if the left number is less than or equal to the right, and returns true if it is. |
[$a-le $b] returns TRUE. |
List of Boolean operators
List of string operators
Operator |
Description |
Example |
= |
Detects whether two strings are equal and returns true for equality. |
[$a = $b] returns FALSE. |
!= |
Detects whether two strings are equal and returns true if they are not equal. |
[$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 and does not return true for 0. |
[-Z $a] returns true. |
Str |
Detects whether the string is empty and does not return true for null. |
[$a] returns TRUE. |
List of file test operators
Operator |
Description |
Example |
-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. |
[-B $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 if 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 if the file has a SGID bit set, and returns True if it is. |
[-G $file] returns false. |
-K File |
Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. |
[-K $file] returns false. |
-P File |
Detects if the file is a named pipe, and returns True if it is. |
[-P $file] returns false. |
-U file |
Detects if the file has a SUID bit set, and returns True if it is. |
[-U $file] returns false. |
-R File |
Detects if the file is readable and returns true if it is. |
[-R $file] returns TRUE. |
-W File |
Detects if the file is writable and returns true if it is. |
[-W $file] returns TRUE. |
-X File |
Detects if the file can be executed and, if so, returns True. |
[-X $file] returns TRUE. |
-S file |
Detects whether the file is empty (the file size is greater than 0) and does not return true for null. |
[-S $file] returns TRUE. |
-E File |
Detects whether the file (including the directory) exists and, if so, returns True. |
[-e $file] returns TRUE. |
Local variables
Func5 () {
Locally temp=$[$value +3] Local keyword, defining a locals variable
result=$[$temp * 2]
}
Value=6 by default, any variable defined in the script is a global variable that can be accessed within a function
Exit
Exit is a library of functions that exits the shell and returns the given value
In general, exit 0 means that the program ends normally
If exit is not a value of 0, the program produces an error.
In the case of the shell, after calling your program in the shell, use echo $? command to see the exit value of your program. In a shell script, it is usually based on the last command's $? Value to perform some process control
Cd/home/lftest | | Exit 1 Enter the directory where the script is located or exit
Return
The return value of the function, the statement after return no longer executes
#!/bin/bash
Fun () {
if [$ = "1"];then
echo "Arg=1"
Return 1
Else
Return 2
Fi
echo "345" #不会再执行
}
Fun 1
echo $?
Output: arg=1
1
Shell Scripting Learning Record