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

5 篇文章 0 订阅
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值