Читать книгу Linux Bible - Christopher Negus - Страница 209

The ″for…do″ loop

Оглавление

Loops are used to perform actions over and over again until a condition is met or until all data has been processed. One of the most commonly used loops is the for…do loop. It iterates through a list of values, executing the body of the loop for each element in the list. The syntax and a few examples are presented here:

 for VAR in LIST do { body } done

The for loop assigns the values in LIST to VAR one at a time. Then, for each value, the body in braces between do and done is executed. VAR can be any variable name, and LIST can be composed of pretty much any list of values or anything that generates a list.

 for NUMBER in 0 1 2 3 4 5 6 7 8 9 do echo The number is $NUMBER done for FILE in `/bin/ls` do echo $FILE done

You can also write it this way, which is somewhat cleaner:

 for NAME in John Paul Ringo George ; do echo $NAME is my favorite Beatle done

Each element in the LIST is separated from the next by white space. This can cause trouble if you're not careful because some commands, such as ls -l, output multiple fields per line, each separated by white space. The string done ends the for statement.

If you're a die-hard C programmer, bash allows you to use C syntax to control your loops:

 LIMIT=10 # Double parentheses, and no $ on LIMIT even though it's a variable! for ((a=1; a <= LIMIT ; a++)) ; do echo "$a" done

Linux Bible

Подняться наверх