1: Script start line #!/bin/bash This line indicates that the script needs to run with the bash shell, regardless of the user's choice of the interactive shell. Because each shell's syntax is very different, this sentence is very important. 2: Variable definition variable name can only be the beginning of a letter, may contain letters, numbers underline. You can't use a keyword in bash. variable= "variable" variable name and variable value cannot have spaces between single and double quotation marks, the single quotation mark will not translate the content, will be output double quotation marks will be translated according to the original content, the variable will be the value of the variable output using the variable
The
standard procedure to add the $ symbol before the $variable is ${variable} {} is used to differentiate the boundary
If you want to follow a variable with another variable, you should use standard practice
Exporting a variable export variableName meaning is just like defining a global variable. 3: Special variable $$ the process ID of the executing script $n input parameter "the first parameter is the second parameter. [email protected] All parameters entered $* all parameters entered when the $* is surrounded by quotes means that all parameters will be a whole "$ ..." $# the total number of input parameters $? The return value of the previous command $HOME home directory $PATH path 4: Arithmetic operations
+-*/% can be done, pay attention to multiply the time to use \*
Let
result=a+b Note space
result= ' expr $a + $b ' Note spaces
both of the above are available for Operation
Logical operation numeric operation-eq equal-ne unequal-gt greater than-lt less than-ge greater than or equal to-le less than equals string = two strings equal! = Whether unequal-Z string is length 0 (zero)-N length is not a 0 str character whether the null file is tested if the-D is a normal file-R whether it is a readable-w-----whether writable-X can be executed if the-s file size is greater than 0 5: Conditional judgment IF/THEN/FI if/then/else/fi if/then/elif/then/else/fi if [[expression]]; Then statement: (if there is no content with a colon dot) fi judging condition with a space between the brackets, otherwise bash will be considered to be a hyphen a=10 if [[A-eq 10]]; Then echo "A is equal" elif [[A-gt 10]]; Then echo ' A is bigger than ' else Echo ' A is LessThan ' FI 6:CASE/ESAC case variable in CASE1 Ment1;; CASE2) Statement2;; *) Statement3;; Esac 7: loop for loop for variable in list do ... done for loop in 1 2 3 4 5 do echo $loop done While loop while the Boolean condition do ... done if the condition is true then intermittent execution until loop until execution condition do ... done as If the condition is false, then the intermittent execution jump out of the loop break, the continue break can jump out of all loops, if the nestedLoop, followed by a number after break to follow the first layer of the loop for var1 in ARR1 does for var2 in arr2 do if [[Condition]];then break 2 fi] Done doing the above example break 2 is directly out of the outermost loop 8: function function_name () {List of commands [return value]} function can pass parameters
Shell Quick Start