Bash Foundation
Basics:
Variable: variable name + memory space pointed to; variable assignment: var_name=value; variable type: data storage format, data range represented; character type, numeric type, Boolean type; C voice is a strong variable type (type must be declared before use, cannot be changed after type declaration) Bash is a weak variable type (no prior declaration is required, the type varies with requirements); the default variable type of bash is character type; to perform arithmetic operations, it needs to be declared as numeric: declare-I var_name variable reference: ${var_name} $var_name
Arithmetic operation:
Let VAR=expression; VAR= $[expression]; VAR= $((expression)); VAR= $(expr argu1 argu2 argu3) Note: sometimes multiplication symbols need to be escaped. Enhanced assignment: + =,-=, * =,% = variable is saved to this variable after some operation let i=$i+n-- > let iMUBQN-> this let variable does not need to appear $let iComple1-> let iTunes + let iMuk conditionals: (1) execute commands. And judge by the return status of the command, $? 0: successful 1 status return value of failed script: default is to return the last named status return value custom exit status code: exit [n]: n is the specified status code (2) Test expression test expression [expression]-note that expression must have a blank character [[expression]] numeric test:-eq: whether it is equal to [$sum1-eq $sum2]-ne: not equal to-gt: whether to hit-ge: whether greater than or equal to-lt: whether less than-le: whether less than or equal to characters test: = =: equal to >: greater than-a >-o Change the level in parentheses Escape characters should be used in parentheses, leaving spaces before and after
Example:
| echo "0" ["is a good day" = ~ "day"] & & echo "1" | | echo "0" [- a $file] & & echo "1" | | echo "0" [root@localhost tmp] # bash-x logic_script.sh + file=/tmp/logic_scritp.sh-- look | Logic | If the front is true, the latter will not execute +'[6-lt 7']'+ echo 11 +'['isagoodday' = ~ 'day'] 'logic_script.sh: line 6: [: = ~: binary operator expected-string comparison requires two parentheses [[]] + echo 0 +' ['- a'/ tmp/logic_scritp.sh] 'logic_script.sh: line 7: [: missing `]-look at the error prompt.] there are no spaces separated. Changed to missing `]'+ echo 0 after modification: logic_script.sh #! / bin/bash#file=/tmp/logic_script.sh [6-lt 7] & & echo "1" | | echo "0" [["isagoodday" = ~ "day"]] & & echo "1" | | echo "0" [- a $file] & & echo "1" | | echo "0" [root@localhost tmp] # bash-x logic_script.sh + file=/tmp/logic_script.sh+'['6-lt 7']'+ echo 11 + [[isagoodday = ~ day]] + echo 11 +'['- a / tmp/logic_script.sh']'+ echo 11
Variable scope
Environment variable: current shell process and child process valid assignment: (1) export var_name=value (2) var_name=value export var_name (3) declare-x var_name=value (4) var_name=value Delare-x var_name Note: bash contains many environment variables (usually all uppercase) View: export Declare-x, env local variable: the life cycle of the shell that is currently running the script Invalid assignment to child shell: var_name=value reference: ${var_name}, $var_name View: set undo: unset var_name (not $here) Local variable: a snippet of code, such as the context of a function, that acts on the life cycle of a function Destroy at the end of the function Assignment: local var_name=vaule position parameter variable: the parameter passed by the shell process currently executing the script is used: myscript argue1 argu2 argu3.-- > assign a value directly to the parameter reference when executing the script: $1, $2, $3. Rotation: shift n rotates the nth parameter to the first parameter, and all the parameters in front of n are kicked out of the special variable: $0: script file name $#: number of script parameters $*: all parameters $@: all parameters-suppose three parameters 1, 2, 3 "*" are written while the script is running, which is equivalent to "1 2 3" (one parameter is passed) "@" is equivalent to "1"2"3" (passed three parameters) $$: the ID number of the current process the script is running $!: example of the ID number of the last process running in the background: sum_scirp.sh #! / bin/bash#declare-I sumecho "the script filename is: $0" echo "the script argu is: $*" echo "the script argu's number is: $#" shift 2-the previous parameter was kicked echo "the script argu is: $*" Echo "the script argu's number is: $#" for I