代码之家  ›  专栏  ›  技术社区  ›  souki

bash循环未正确更新输出

  •  1
  • souki  · 技术社区  · 6 年前

    我在bash中编写了一个测试脚本来测试我的服务器。测试进度百分比将被更新并替换为测试状态,或者 OK ,或 KO

    Test 1/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 2/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 3/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 4/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 5/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 6/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 7/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 8/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    

    Test 1/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 2/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 3/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 4/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 5/8 : 10 channel(s) and 100 stream(s) : OK (3000/3000)
    Test 6/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                  0%
                                                  10.00%
                                                  20.00%
                                                  30.00%
                                                  40.00%
                                                  50.00%
                                                  60.00%
                                                  70.00%
                                                  80.00%
                                                  90.00%
                                                  OK (3000/3000)
    Test 7/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                  0%
                                                  10.00%
                                                  20.00%
                                                  30.00%
                                                  40.00%
                                                  50.00%
                                                  60.00%
                                                  70.00%
                                                  80.00%
                                                  90.00%
                                                  OK (3000/3000)
    Test 8/8 : 10 channel(s) and 100 stream(s) : Processing...
                                                  0%
                                                  10.00%
                                                  20.00%
                                                  30.00%
                                                  40.00%
                                                  50.00%
                                                  60.00%
                                                  70.00%
                                                  80.00%
                                                  90.00%
                                                  OK (3000/3000)
    

    正如您所看到的,一旦图形错误出现,它就会继续发生。还要注意,它发生在随机的时刻,有时,它根本不会出现。

    for ((i=0; i < d; i++));
    do
            echo "Test $((${i} + 1))/${d} : ${channelTab[i]} channel(s) and ${streamTab[i]} stream(s) : ${saveCursor}Processing..."
            sent=0
            received=0
            errors=0
            e=0
            for ((idx = 0; idx < ${channelTab[i]}; idx++));
            do
                    /u/ecmg/bin/simulator ${fileNameTab[i]} > /dev/null 2<&1 &
                    pourcentage=$(bc <<< "scale=2;((${idx}))/${channelTab[i]} *100")
                    echo ${restoreCursor}${eraseEndOfLine}${saveCursor}${pourcentage}%
                    pids[idx]=$!
                    sleep .5
            done
            for pid in ${pids[*]}; do
                    wait $pid
                    results[e]=$(grep -a "stats" "${fileNameTab[i]}.out")
                    ((e=e+1))
            done
            for ((f=0; f < e; f++));
            do
                    res=$(echo "${results[f]}" | grep -o -E '[0-9]+')
                    resArray=($res)
                    ((sent=sent + ${resArray[0]}))
                    ((received=received + ${resArray[1]}))
                    ((errors=errors + ${resArray[2]}))
            done
            if [ "$sent" != "$received" ];
            then
                    echo -e "${restoreCursor}${eraseEndOfLine}${red}KO${normal} --> ${errors} errors (${received}/${sent})"
            fi
            if [ "$sent" == "$received" ];
            then
                    echo -e "${restoreCursor}${eraseEndOfLine}${green}OK${normal} (${received}/${sent})"
            fi
    done
    

    ${restorCursor} ${eraseEndOfLine} ${saveCursor} 定义:

    saveCursor=$'\033[s'
    restoreCursor=$'\033[u'
    eraseEndOfLine=$'\033[K'
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Nahuel Fouilleul    6 年前

    似乎是由于终端的滚动,当在屏幕底部打印新行时,行会移动到顶部。

    解决方法可能是 printf echo -n 以避免打印换行。