我用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'
你知道为什么会这样吗?
答案 0 :(得分:1)
似乎是由于终端的滚动所致,当在屏幕底部打印换行符时,这些行将移至顶部。
一种解决方法是使用printf
或echo -n
以避免打印换行符。