- #!/bin/sh
- diffcount=0
- for fn in `ls /var/named/chroot/var/tmp/hosts`
- do
- diffcount=$((diffcount +1))
- [ $diffcount -gt 500 ] && break
- #if [ $diffcount -gt 500 ]
- #then
- # break
- #fi
- done
- echo $?
- echo $diffcount
上述输出结果为1和151,从151可以得知没有运行break;若换成
- #!/bin/sh
- diffcount=0
- for fn in `ls /var/named/chroot/var/tmp/hosts`
- do
- diffcount=$((diffcount +1))
- #[ $diffcount -gt 500 ] && break
- if [ $diffcount -gt 500 ]
- then
- break
- fi
- done
- echo $?
- echo $diffcount
则输出为0和151;
两种方法其实表示的是一致的行为,也就是大于500的时候退出for循环;但是for循环输出的值却是不一样的,然后在论坛上请教其他人:
- for name [ in word ] ; do list ; done
- The list of words following in is expanded, generating a list
- of items. The variable name is set to each element of this
- list in turn, and list is executed each time. If the in word
- is omitted, the for command executes list once for each posi-
- tional parameter that is set (see PARAMETERS below). The
- return status is the exit status of the last command that exe-
- cutes. If the expansion of the items following in results in
- an empty list, no commands are executed, and the return status
- is 0.
从而得知for循环的返回值是最后一个命令执行的结果,第一种情况最后一个命令是[ 151 -gt 500 ]返回的一定为1,而第二种情况没有命令,所以默认的是返回0;同理if也是这样的情况
阅读(1722) | 评论(0) | 转发(0) |