您的位置:首页 > 运维架构 > Shell

How Does Shell Script Looping Work?(shell程序设计中的循环)

2011-09-04 12:04 477 查看
Shell scripts written in Bash can implementlooping, or iteration, with thewhile,until, andforconstructs. In each case, a block of code is executed repeatedly
until a loop exit condition is satisfied. The script then continues on from that point.

The while Statement

In awhileloop, the block of code between thedoanddonestatements is executed so long as the conditional expression is true. Think of it as saying, "Executewhilethis condition remains true." Here's
an example:

while [ "$*" != "" ]

do

echo "Argument value is: $1"

shift

done


This trivial example prints the value of each argument passed to the shell script. Translated to English, thewhilecondition says to continue so long as the input argument string is not null. You could also code thewhilestatement
as

while [ -n"$*"]

but I think the first method is much easier to read and understand.

You might think that this loop would continue to print the first argument ($1) forever and ever, since you don't expect the value of the$*variable (the list of arguments from the command line) to change during the course
of running the script. You'd be right, except that I slipped theshiftcommand into the body of the loop.

Whatshiftdoes is discard the first argument and reassign all the$nvariables--so the new$1variable gets the value that used to be in$2,and so on. Accordingly, the value
in$*gets shorter and shorter each time through the loop, and when it finally becomes null, the loop is done.

The until Statement

Theuntilconstruct works almost exactly the same aswhile. The only difference is thatuntilexecutes the body of the loop so long as the conditional expression is false, whereaswhileexecutes
the body of the loop so long as the conditional expression is true. Think of it as saying, "Executeuntilthis condition becomes true."

Let's code the previous example using anuntilloop this time and making it a little fancier by adding acountervariable:

count=1

until [ "$*" = "" ]

do

echo "Argument number $count : $1 "

shift

count=`expr $count + 1`

done


Again, you could have coded theuntilstatement as

until [ -z"$*"]

but I recommend not using the-nand-zoperators because it's harder to remember what they do.

The only new concept here is the strange-looking line that increments the counter:

count=`expr $count + 1`

Theexprcommand signals to the shell that we're about to perform a mathematical calculation instead of a string operation. And the doodads that look kind of like single quotation marks are not--they're thebacktick(`)
character, found to the left of the number 1 key on most keyboards. By enclosing an expression in backticks, you tell the shell to assign the result of a Linux command to a variable, instead of printing it to the screen.

Note: The spaces on either side of the plus sign are required.

The for Statement

Theforstatement is yet another way to implement a loop in a shell script. The general form of theforconstruct is shown here:

for item in list

do

something useful with $item

done


Each time through the loop, the value of theitemvariable is assigned to thenth item in the list. When you've processed all the items in the list, the loop is done. Here's an example similar to theuntilandwhileloops
you saw earlier in this section:

for item in"$@"

do

echo "Argument value is: $item"

done


Note that in this example, I used the special variable$@instead of$*as the list of arguments. The variables$@and$*both contain the command line arguments, but theforcommand
expects an array (a list of items) as input.

Read more:http://lowfatlinux.com/linux-script-looping.html#ixzz1Wx9zHh6w
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: