For, while, until in the shell

Source: Internet
Author: User
Tags lenovo

 for var inch List  Do     Commandsdone

In each iteration, the variable var contains the current value in the list, the first iteration uses the first value in the list, and the second one uses the second value.

In do and done, the $var variable contains the current value corresponding to this iteration.

You can put the do statement and the FOR statement on the same line as long as you want, but you must separate it from the values in the list with a semicolon. for Var in list;do.

#! /bin/bash for in Alabama Alaska Arizona Arkansas California Colorado do        echo  the is $testdone

After the last iteration, the value of the $test variable remains valid for the remainder of the shell script. It will keep the value of the last iteration (unless you modify it).

The shell sees single quotes in the for list values and attempts to use them to define a single data value, a process that is chaotic.

Two solutions:

1) Use escape character (backslash) to escape single quotes

2) Use double quotation marks to define the value used in single quotation marks.

Such as:

 for inch I Don't  know ifthis'll     work does'Word : $test"Done" results: [email protected]-lenovo:~/shell_test$./for_test Word:iword: Dont  if  thisllword:work

After modification

 for  test in  I don\"  t know if "This  "  Ll "  work  do  Span style= "COLOR: #000000" > echo   " word: $ Test   done execution result: [email protected] -lenovo:~/shell_test$./for_test Word:Iword:don   " t  word:knowword:  if  word:  this   " ll  word:work 

The For loop assumes that each median value is separated by a space. Note: If you have a data value that contains spaces, you can enclose it in double quotation marks, and the shell does not use double quotes as part of the value.

Reading a list from a variable

Typically a shell script encounters a situation where a series of values are concentrated in one variable, and then the entire list needs to be traversed.

list="Alabama Alaska Arizona Arkansas Colorado"list= $list"  Connecticut"for in $list does"has You ever visited $state? "  Done

Results:

Have you ever visited Alabama? Have you ever visited Alaska? Have you ever visited Arizona? Have you ever visited Arkansas? Have you ever visited Colorado? Have you ever visited Connecticut?

Note: The code uses another assignment statement to add a value to the existing list that the $list variable contains. This is a common way to add text to the end of an existing text string stored in a variable.

Reading a value from a command

Use the anti-quote to execute any command that produces the output. Enter some parameters in the states file.

file="states" for in ' cat $file '      do " Visit Beautiful $state "  Done

In Linux, it is certainly legal to include spaces in directory names and filenames, and to accommodate such values, you should enclose the $file variable in double quotes.

Multiple lines of comments in shell scripts:

if false ; Then statement 1 Statement 2 Statement 3 statement 4fi

Such as:

#! /bin/bashiffalse;  Alabama Alaska Arizona Arkansas California Coloradodo  echothe is  $testdonefi

For, while, until in the shell

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.