Basic knowledge
Shell execution Mode SH xx.sh (file must have R permission) | | ./xx.sh (file must have X permission) Note: Sh xx.sh method is more safe
FileName suffix. sh
Shell content Opening #!/bin/bash | | #!/bin/sh | | #!/bin/csh | | #!/bin/ksh Note: #!/bin/bash use more
Exit Exit 0
Variable assignment and use
You can use a note without a definition: When a variable is assigned a value = no space at both ends
Access variable contents with $ variable name or ${variable name}
Process Control
Judgment Note: Ensure the square brackets of the space; Make sure every condition has a then.
Copy Code code as follows:
If [–d ' DirName '];then
Command Set 1
Elif Conditions 2;then
Command Set 2
Else
Command Set 4
Fi
Copy Code code as follows:
Case $ variable in
match_1)
Command Set 1
;;
match_2)
Command Set 2
;;
......
*) (optional)
Command Set N
;;
Esac
Cycle
1.while conditions
Copy Code code as follows:
2.until conditions
Copy Code code as follows:
3.for ((initial value; condition; step)
Copy Code code as follows:
4.for loop variable in variable list
Copy Code code as follows:
Common Judgment Condition Statement
Determining File Types
Examples of test markers
-E "file or directory" exists test–e $file _name
-F "file or directory" exists and is a file test–f $file _name
-d "file or directory" exists and is directory Test–d "/boot"
-B "file or directory" exists and is a block device Test–b "/DEV/SDA1"
-C "file or directory" exists and is a word device Test–c "/dev/tty0"
-S "file or directory" exists and Test–s "/var/run/rpcbind.sock" for the socket file
-P "file or directory" exists and is a FIFO file test–p $file _name
-L "file or directory" exists and is a linked file test–l $file _name
Determine file permissions
Test flag semantics
-r "file or directory" exists and has readable permissions
-W "file or directory" exists and has writable permissions
-X "file or directory" exists and has executable permissions
-U "file or directory" exists and has suid properties
-G "File or directory" exists and has sgid properties
-k "file or directory" exists and has sticky bit attributes
-S "file or directory" exists and is not an empty file
Comparison between files
Test flag semantics
-nt "FileName 1" is newer than "FileName 2" (newer than)
-ot "FileName 1" is older than "FileName 2" (older than)
-ef to determine whether "file name 1" and "FileName 2" are the same file, mainly to determine the hard link, that is, two files point to the same inode node
Comparison between integers
Test flag semantics
-eq two number equal (equal)
-ne two numbers (not equal)
-GT integer 1 is greater than integer 2 (greater than)
-lt integer 1 is less than integer 2 (less than)
-ge integer 1 is greater than or equal to integer 2 (greater than or equal)
-le integer 1 is less than or equal to integer 2 (less than or equal)
And, or, non-
Test flag semantics
-A (and) two conditions are set to return True Test–e file1–a–d file2
O (or) two conditions any one is set to return True Test–r file1–o–x file2
! (not) The current condition is not test! –d file1
Control structure
To calculate an integer variable value
Expr Note: There is a space between the operation symbol and the parameter, and the wildcard symbol (*) is used as the multiplication operator;
Let example: Let S= (2+3) *4