[Linux] structured command-for

Source: Internet
Author: User

1 for command

1  for : iteration loop; default space is delimiter 2 3  for var inch List 4  Do 5     Commands6 Done

1.1 reading the values in the list

#!usr/bin/bash for in Hello Python Student School    does is is $test # keeps the value of the last iteration until it is deleted (or changed)  

1.2 Reading complex values in a list

 1  # use escape character (backslash), escape special character  2  # use double quotation marks to define the single quotation mark (or vice versa)  3  # Default space is delimiter 4  #!usr/bin/bash  5  for  test in  I don\ " t know if" This  "  ll "  work  6  do  7  Echo Span style= "COLOR: #800000" > " word: $test  "  8  done 
# double quotes Create a string variable and appear as a whole in the list $cat fortest01.sh#!usr/bin/Bash for var inch "The Test bash Shell" Doecho Word: $vardone$sh fortest01.shword:the Test bash Shell$cat fortest01.sh#!usr/bin/Bash for var inch "The Test bash Shell" "The last test" Doecho Word: $vardone$sh fortest01.shword:the Test bash shellword:the last Test

1.3 Reading a list from a variable

1 # Note the difference from the direct read list:2 # list:3*escape characters for special characters in the list4* Double Quotes-special characters and string variables (overall)5 # variable6* Define string variables-double quotes/single quotation marks7*string add element8*For Iteration traversal variable element9 $cat varfortest02.shTen#!usr/bin/Bash Onelist01='The first test example' AList02="The Second Test example" -list03= $list 02"The thrid Test Example" -list04=$list 01$list02$list03 the Echo $list -n=0 -  for var inch$list -  Do +((n++ )) -Echo Cycle $n: $var +  Done A  at $sh varfortest02.sh - The first test example the second Test example the second Test example the Thrid test example -Cycle1: the -Cycle2: First -Cycle3: Test -Cycle4: Example inCycle5: the -Cycle6: Second toCycle7: Test +Cycle8: Example -Cycle9: the theCycleTen: Second *Cycle One: Test $Cycle A: ExamplePanax NotoginsengCycle -: the -Cycle -: Thrid theCycle the: Test +Cycle -: Example

1.4 reading a value from a command

1 # anti-quote2*use the absolute path of the file, unless you are in the same directory3*default space is delimiter4 $cat commandfor.sh5#!usr/bin/Bash6n=07  for var inch' Cat varfortest02.sh '8  Do9n=$[$n +1 ]TenEcho Line $n: $var One  Done A  - $sh commandfor.sh -Line1: #!usr/bin/Bash theLine2: list01=' the -Line3: First - ... ... -Line -: Do +Line -: (( -Line -: n++ +Line -: )) A ... ... atLine to: $var -Line +: Done

1.5 changing field separators

1# environment variable ifs-internal Field delimiter2*default delimiter: spaces; tabs; line breaks3* Change: Single: ifs=$'\ n'-(line wrapping)4Multiple: ifs=$'\ n:; "'-(newline/colon/semicolon/double quotes)5*Save and Restore:6Ifs. Old=$IFS7ifs=$'\ n'8                 ...9ifs=$IFS. oldTen  One $cat commandfor.sh A#!usr/bin/Bash -n=0 -Ifs. Old=$IFS theifs=$'\ n' -  for var inch' Cat varfortest02.sh ' -  Do -n=$[$n +1 ] +Echo Line $n: $var -  Done +ifs=$IFS. old A  at $sh commandfor.sh -Commandfor.sh:line3: IFS. Old=: Command not found -Line1: #!usr/bin/Bash -Line2: list01='The first test example' -Line3: list02="The Second Test example" -Line4: list03= $list 02"The thrid Test Example" inLine5: list04=$list 01$list02$list03 -Line6: Echo $list toLine7: n=0 +Line8: for var inch$list -Line9: Do theLineTen: ((n++ )) *Line One: Echo Cycle $n: $var $Line A: Done

1.6 reading a file/directory with wildcard characters

1 # File/directory variables are enclosed in double quotation marks as far as possible.2 # File/directory lookup method and list method merge into the same for statement
* You can add any number of wildcards, the for statement first matches a file or directory to form a list, and then iterates through the list3 $cat filefor.sh4#!usr/bin/Bash5 forFileinch./*./TSTTST6 Do7 if [-E "$file"]8 Then9 echo the file is $fileTen Else One echo the Fiel $file do not exist! A fi - Done - the $sh filefor.sh - The file is./commandfor.sh - The file is./filefor.sh - The file is./fortest01.sh + The file is./varfortest02.sh - The fiel./tsttst do not exist!

[Linux] structured command-for

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.