This article focuses on the parts of the bash script that are prone to errors and rarely used but have unexpected effects.
Cycle:
Normal for loop:
for inch a B C 1 2 3 do Echo " $i " Done
Number sequence loop:
for in 'seq-do- echo'$i'done
The 'seq ' can also be replaced with {1. +}
{Start: This form of the end of the} can represent a continuous sequence of numbers, from small to large can be large to small, but unfortunately not like the Python range () can specify the amount of space
C language-style for loop:
for ((i=1; i< "$i"Done
Mathematical calculations
The Double Parenthesis "(())" can also be used for mathematical calculations:
((a=123+456 )) Echo $a # 579
In double brackets, the variable does not need to be referenced by the $ symbol, and the English word is naturally a variable.
Variable: Indirect reference:
A=bb=echo ${! a}#
The effect is the same as the eval echo \$ $a
String manipulation
The character length of the variable value:
A="abcdefg"echo ${#a}#7
Variable Value character interception:
A="abcdefg"echo ${a:2}# CDEFGecho ${a: 2:3}# CDE
Variable Value character deletion: (#为从头部开始匹配,% to match from tail, support wildcard "? *”)
A="abdcdefgh"echo ${a#*d} # Remove the part of the string header that matches "*d", the wildcard * stands for matching any number of characters, and a # number represents the wildcard "*" Conservative match # cdefghecho ${a##*D} # two # # denotes greedy match # efghecho ${a%d*} # Ibid, two% when it means greedy match # ABDC
A=bb="abcdefg"echo"${!a#abc} "# DEFG
can support both indirect and character deletion
Variable Assignment security
Used to check whether a value has been defined or assigned before it is assigned.
unset aunset BB=""# A has no definition, B defines but a null valueEcho${a-789} # If not defined, the expression takes the default value 789# 789Echo${a:-789} # If undefined or null, the expression takes the default value 789# 789Echo${b-789} # If not defined, the expression is defined with the default value 789,b # Echo${b:-789} # If undefined or null, the expression takes the default value 789# 789Echo$a # The original variable is not assigned a value #Echo$b # The original variable is not assigned a value#b=123Echo${b-789# If the variable is already assigned, the original value is not changed # 123
unset a echo ${a=789} # If A is undefined, the expression takes the default value and assigns a value to a #789echo $a # 789 # A has been assigneda =""echo ${a=789}#echo ${a : =789} # Plus colon: function as before #789echo $a #789
In front of the use of "-" and "=", there is a "+", for when the variable has been assigned to use the default value, extrapolate, do not repeat.
Positional variables
$ A is a file name, $ #代表参数个数, $1,$2, and so on. Represents the position parameter followed by the script or function, representing the first argument, and so on, it should be noted that the 9th parameter should be represented as follows:
${}
This means that the 10th argument must be enclosed in curly braces, otherwise it becomes the 1th variable followed by the character "0". Because some people think that the 9th position after the variable is not available, is wrong.
[email protected] and $* all represent all variables, but the difference is that each as a string, is a combination of multiple strings, while the latter combines all the variables into a single string, separated by a space.
This difference will cause the use differences within the For loop, in the For loop, if you enclose the quote for i in "[email protected]" and for i in "$*" is different, the former will be in turn the parameters into the loop, and the latter will. All parameters are entered as strings at once:
Echo ' forIinch "[email protected]"; Do Echo "$i" Done forIinch "$*"; Do Echo "$i" Done'> Myscript.shchmod 755MyScript.SH./myscript.SHAbc123def
The result is:
ABC 123 123 def
So [email protected] is there a difference between quoted and unquoted?
Most of the time the results look the same, but in some cases it can be troublesome because the for loop splits the variable with a newline character "\ n" and a space, so if the calling script takes a space or line break in the argument, the argument is disassembled and then replaced in turn, This can be avoided by adding quotation marks.
Echo ' forIinch "[email protected]"; Do Echo "$i" Done forIinch[Email protected]; Do Echo "$i" Done'> Myscript.shchmod 755MyScript.SH./myscript.SHAbc"123 def"
The result is:
ABC 123 DEFABC 123 def
For loops using arrays
The values of the array are sequentially in the loop, which can be used ${arr[@} and ${arr[*]}, and the difference between the differences and the quotation marks is the same as the use of the above positional parameters.
String match
The Shell's string match is usually the case:
" ABC " " ABC " ]echo0
Returns 0 if the string is the same, otherwise returns 1. In addition to the full match, the regular expression can also be used to match the "[[]]" by the two brackets "[[]]".
" Abc234def " =~ ^abc[0-9]{3}[a-z]+$]]echo0
Note that the regular expression cannot be enclosed in quotation marks, and only supports basic regularization, and does not support extensions such as "\b \s".
Original address: http://www.cnblogs.com/foxgab/p/6901782.html
If you feel that this article is helpful, please scan the following QR code to give a donation, your support is the author continues to write a better article power!
Linux shell Programming-bash artifice