Linux Shell Learning Notes-variable declaration and assignment, looping

Source: Internet
Author: User

Bash variables are non-typed------------------------Unlike other programming languages, Bash does not differentiate between variables"type"In essence, bash variables are strings. But depending on the context, bash also allows comparison operations and arithmetic operations. The key factor in determining these is whether the values in the variables are only numbers. Assignment a=123a="SDSD"#注意 =do not have a space around, otherwise it will become a comparison string operation reference $ A #借助 $ character action string characters wear connection a="SDFs"b="DSDS"C=$a $b--------------Bash supports extra-large string operations with amazing types and numbers of operations. Unfortunately, these tools lack concentration. Some are subsets of parameter substitutions, but others are Unix's expr commands. This leads to inconsistent command syntax and overlapping functions, Of course it can cause confusion. String length ${#string}ExprLength $stringExpr "$string":'.*'1stringz=ABCABC123ABCABC23 Echo${#stringZ} # the4 Echo`ExprLength $stringZ ' # the5 Echo`Expr "$stringZ":'.*'` # thematches the length of the substring from the beginning of the stringExprMatch"$string" '$substring'$substring is a regular expressionExpr "$string":'$substring'$substring is a regular expression1stringz=ABCABC123ABCABC2# |------|34 Echo`ExprMatch"$stringZ" 'abc[a-z]*.2'` #85 Echo`Expr "$stringZ":'abc[a-z]*.2'` #8ExprIndex $string$substring the position that matches the first character of the substring.1stringz=ABCABC123ABCABC2 Echo`ExprIndex"$stringZ"C12 ' #63# C position.45 Echo`ExprIndex"$stringZ"1c ' #36#'C'(inch#3Position) matches before'1'The nearest equivalent function in C is STRCHR (). Extract substring ${string:p osition} instringextracts the substring from position $position. If $stringFor"*"Or"@", the positional parameters starting from position $position are extracted, [1]${string:p Osition:length} instringThe $length length substring is extracted from the position $position. ############################### #StartScript ################################# ######1stringz=ABCABC123ABCABC2#0123456789.....3#0-based indexing.45 Echo${stringz:0} # ABCABC123ABCABC6 Echo${stringz:1} # BCABC123ABCABC7 Echo${stringz:7} # 23ABCABC89 Echo${stringz:7:3} # 23ATen#3Substring with a character length. substring removal ${string#substring} from $stringto the left to intercept the first matching $substring${.string# #substring} from $stringto the left to intercept the last matching $substring.1stringz=ABCABC123ABCABC2# |----|3# |----------|45 Echo${stringz#a*C} # 123ABCABC6# Cut it off #'a'And'C'the closest match between the.78 Echo${stringz# #a *C} # ABC9# Cut it off #'a'And'C'The furthest match between the ${.string%substring} from $stringto the right to intercept the first matching $substring${ .string%%substring} from $stringto the right of the last matching $substring .1stringz=ABCABC123ABCABC2# ||3# |------------|45 Echo${stringz%b*C} # ABCABC123ABCA6# from behind the $stringz, start cutting off'b'And'C'The closest match between78 Echo${stringz%%b*C} # a9# from behind the $stringz, start cutting off'b'And'C'The furthest matched substring between the replacement ${string/substring/Replacement} replaces the first matching $substring.${with $replacementstring//Substring/replacement}use $replacement to replace all matching $substring.1stringz=ABCABC123ABCABC23 Echo${stringz/abc/xyz} # XYZABC123ABCABC4# with'XYZ'To replace the first one that matches the'ABC'.56 Echo${stringz//abc/xyz} # xyzabc123abcxyz7# with'XYZ'To replace all of the matching
Personally feel that the format requirements of the conditional decision box is very strict, ' [' must have a space if structure if [Condition1]then if [condition2] then do-something # here only in Conditi    On1 and Condition2 are available. Fielif [Condition3]then do ... else do ..... ficase structure note to;; End case "$variable" inabc) echo "\ $variable = abc";; XYZ) echo "\ $variable = xyz";;  *) echo "DFSDF";; #* represents default, and if none of the above matches is successful, the ESACFOR structure is executed. It is a basic loop structure. It differs greatly from the similarity structure of C. # Standard syntax. For a in 1 2 3 4 5 6 7 8 9 10do echo-n "$a" done Limit=10for ((a=1; a <= limit; a++)) # Double parentheses, and ' LIMIT ' with no ' $ '. for ((a=1; a <= limit; a++)) # Double parenthesis, and there is no "$" in front of the "LIMIT" variable. Do echo-n "$a" donewhile [Condition]do Command...done and for loops, if you want to put do and conditions on the same line or need a ";" . while [condition];   DOC-style syntax while ((a <= LIMIT) # Double parenthesis, there is no "$" in front of the variable. Do echo-n "$a" ((A + = 1)) # let "A+=1" # Yes, see. # double parenthesis allows you to increase the value of a variable as if it were a C-style syntax. DONEUNTIL This structure determines the condition at the top of the loop and, if the condition is always false, keeps circulating. Until [Condition-is-true]docommand...done Note: The judgment of the until loop is at the top of the loop, which is different from some programming languages. As with a For loop, if you want toand conditions on one line, use ";". until [condition-is-true]; Do

Linux Shell Learning Notes-variable declaration and assignment, looping

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.