Chinaunix首页 | 论坛 | 博客
  • 博客访问: 567529
  • 博文数量: 84
  • 博客积分: 1529
  • 博客等级: 上尉
  • 技术积分: 1482
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-27 17:57
文章分类

全部博文(84)

文章存档

2014年(7)

2013年(9)

2012年(20)

2011年(48)

分类: Python/Ruby

2011-08-24 16:48:03

  1. #!/bin/sh
  2. diffcount=0
  3. for fn in `ls /var/named/chroot/var/tmp/hosts`
  4. do
  5. diffcount=$((diffcount +1))
  6. [ $diffcount -gt 500 ] && break
  7. #if [ $diffcount -gt 500 ]
  8. #then
  9. # break
  10. #fi
  11. done
  12. echo $?
  13. echo $diffcount

上述输出结果为1和151,从151可以得知没有运行break;若换成

 

  1. #!/bin/sh
  2. diffcount=0
  3. for fn in `ls /var/named/chroot/var/tmp/hosts`
  4. do
  5. diffcount=$((diffcount +1))
  6. #[ $diffcount -gt 500 ] && break
  7. if [ $diffcount -gt 500 ]
  8. then
  9. break
  10. fi
  11. done
  12. echo $?
  13. echo $diffcount

则输出为0和151;

两种方法其实表示的是一致的行为,也就是大于500的时候退出for循环;但是for循环输出的值却是不一样的,然后在论坛上请教其他人:

 

 

  1. for name [ in word ] ; do list ; done

  2.               The list of words following in is expanded, generating a list

  3.               of items. The variable name is set to each element of this

  4.               list in turn, and list is executed each time. If the in word

  5.               is omitted, the for command executes list once for each posi-

  6.               tional parameter that is set (see PARAMETERS below). The

  7.               return status is the exit status of the last command that exe-

  8.               cutes. If the expansion of the items following in results in

  9.               an empty list, no commands are executed, and the return status

  10.               is 0.
从而得知for循环的返回值是最后一个命令执行的结果,第一种情况最后一个命令是[ 151 -gt 500 ]返回的一定为1,而第二种情况没有命令,所以默认的是返回0;同理if也是这样的情况
阅读(1631) | 评论(0) | 转发(0) |
0

上一篇:getopts

下一篇:php并发处理

给主人留下些什么吧!~~