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