In the shell script, the variable definition, the reference each has the different way, in addition, very commonly has the file attribute judgment, the logical operation, the numerical operation and so on, the following record their attribute function
Variable
Shell Name=: Direct definition assignment Time=< span class= "str" > ' date ' : instruction nesting assignment $name $time : call variable also variables and global variables, local variables only for the current bash environment effective, environment variables globally valid, the above definition is defined as local variables, the global variable is defined as follows Span class= "PLN" > Name=yufu expor name env | grep name : View environment variables unset name : Cancel environment variable
Positional variables and state variables
$ $...$9: Receive parameters from standard input${10} : 10 or more parameters enclose the values in curly braces.$* : All parameters: Call as a parameter when calling between scripts [email protected] : equals $ * effect : parameters can be called separately when calling between scripts $$0: Show script name set Span class= "pun" >-- : Clear position parameter shift : parameter is shifted to the left one bit by default, you can specify the number $?: Instruction execution status value 00
Numerical Operation method
X=1y=2z=$[x+y] or let z=x+ or Z=$((x+y))
Logical judgment
True, False.Logical operation:and operations:Really && Really = ReallyReally && False = FalseFalse && Really = FalseFalse && False = FalseOR operation:Really || Really = ReallyReally || False = Really false | | true = True false | | false = False non-op: ! true = false ! false = true
Logic andCmd1&&Cmd2IfCmd1is False,Cmd2Does not need to be executed ifCmd1Is true,Cmd2To perform Logical ORCmd1||Cmd2IfCmd1is false, then to executeCmd2If it is true, thenCmd2No execution Boolean operations Numerical method True, False 1 0 And: 1 And 1 = 1 1 And 0 = 0 0 And 1 = 0 0 And 0 = 0 Or: 1 Or 1 = 1 1 Or 0 = 1 0 Or 1 = 1 0 Or 0 = 0 Different way True, False. and operations: Really && Really = Really Really && False = False False && Really = False False && False = False OR operation: Really || Really = Really Really || False = Really false | | true = True false | | false = False non-op: ! true = false ! false = true
An example of a logical operation
[~]# ID yufu &>/dev/null && echo "user exist" | | useradd yufuuser exist during#在使用 and or logical operation , if the preceding instruction executes correctly, then the subsequent operation instruction is executed, and if the preceding instruction fails (the state value is not 0) then the subsequent operation will not execute, skipping the execution or operation instruction #如果或运算在前面, the rule is the opposite [~]# [-D/ TMP/HHH] | | Mkdir/tmp/hhh
Conditional judgment
Conditional judgmenttest : State value 0 1 false test $str 1 = $str 2 or (common) [ $str 1 = $str 2 ] (common) [ -z $var ] : Judging whether it is empty, File properties can be judged
File judgment
-A: Whether the file exists-D: Whether it is a folder-F: Whether the file exists-L: Soft connect -w: Write permission Span class= "PLN" >-x: Do you have execution privileges -v: Whether the variable is set - o: OR, multi-criteria -n : Whether the variable is empty -z: Determine if the string is empty Span class= "PLN" >[] : Use single brackets when not using regular expressions < Span class= "pun" > using regular expressions with double brackets [[]]
Shell scripts: variables, file judgments, logical operations, etc.