V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
gouera
V2EX  ›  Linux

关于SHELL脚本循环的问题

  •  
  •   gouera · 2013-06-22 14:03:09 +08:00 · 2676 次点击
    这是一个创建于 3954 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚学shell脚本,在书上看到这里的时候有点小疑问。
    #!/bin/bash

    aa=0
    while echo "while iteration: $aa "
    [ $aa -lt 15 ]
    do
    if [ $aa -gt 5 ] && [ $aa -lt 10 ]
    then
    continue
    fi
    echo " inside iteration number: $aa"
    aa=$[ $aa + 1 ]
    done

    运行这个脚本会出现这样的结果:
    [root@test ~]# . a.sh |more
    while iteration: 0
    inside iteration number: 0
    while iteration: 1
    inside iteration number: 1
    while iteration: 2
    inside iteration number: 2
    while iteration: 3
    inside iteration number: 3
    while iteration: 4
    inside iteration number: 4
    while iteration: 5
    inside iteration number: 5
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6
    while iteration: 6


    我想知道,为什么6这里过不去呢。
    2 条回复    1970-01-01 08:00:00 +08:00
    rse43
        1
    rse43  
       2013-06-22 14:06:00 +08:00 via iPad   ❤️ 1
    if [ $aa -gt 5 ]
    gouera
        2
    gouera  
    OP
       2013-06-22 14:10:23 +08:00
    啊,我知道为什么了,
    当$aa=6的时候,
    刚好满足 [ $aa -gt 5 ] && [ $aa -lt 10 ] 这条语句, 然后这个循环又跳到开头去了。 然后因为没有结束的条件。所以就一直这样死循环了。
    本来一直想不通的,现在仔细分析一下,就通了。哈哈
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 09:23 · PVG 17:23 · LAX 02:23 · JFK 05:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.