Shell syntax "If and for"

Source: Internet
Author: User

"If syntax test condition judgment Statement" go from: http://lovelace.blog.51cto.com/1028430/1211353 "while for Loop" go from: 78681117for variable in list do Command1 Command2. Commandndone can also be written as: for Var in list; Do read the values in the list for test in Alabama BOb Tom Consoledo echo the next state is $testdone there are two workarounds for reading the complex values in the list: * Use escape characters (backslashes) to transfer single quotes; * Use double quotation marks To define the value used in single quotation marks for test in I don\ ' t think if "This will" Workdo echo the next state was $testdoneThe next state was ithe next state IS, Don ' TThe next state was thinkthe next state is ifthe next state is the ' this ' llthe next state was work read value from command There are two ways to assign a command output to a variable : (1) the inverse quotation character (') (2) $ () format for example: test= ' Date ' test=$ (date) #!/bin/bash# reading values from a filefile= "states" for state in $ (cat $ File) do echo "visit beautiful $state" donestates files Contents: Alabama bobtom Console execution result: visit beautiful alabamavisit Beautiful Bobvisit beautiful tomvisit Beautiful console field separator The cause of this problem is the special environment variable ifs, called the internal field delimiter. By default, the bash shell treats the following characters as field separators: * Spaces * TAB * tabs * line breaks refer to the security practice of saving the original IFS value before changing the IFS, then recovering it ifs.old= $IFSIFS =$ ' \ n ' < using the new IFS value in the code >ifs= $IFS. Old using wildcards to read file directories for files IN/PRoc/*; Do echo $file is file path \!; The syntax format of the Done Class C-style for loop for ((EXPR1; expr2; expr3)) does command command ... done #!/bin/bash# uses the Class C-style for loop output (integer = 1; Integer <= 5; integer++) do echo "$integer" doing uses the while loop to calculate the and of 1 to 100: The Let command is a tool used in bash to execute one or more expressions, which do not need to be added to represent variables in the calculation of variables. If the expression contains spaces or other special characters, it must be noticed: Bash's tools, if executed with SH cmd.sh script, let command does not recognize I=1sum=0while [$i-le]do let sum=sum+ $i let I++done use Read Combine while loop reading text file # Assign value to Linewhile read-r line # from the file file (using the parameter R will mask the special symbols in the text, only output does not translate) do echo $line #输出文件内容done < $file

Shell syntax "if and for"

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.