我一直在玩一个脚本来通过ping来测试各种VoIP服务器的速度,然后我找到了一个进度条脚本并合并了它......因为酷!现在我正在尝试在状态栏下方显示当前正在测试的服务器,我有覆盖行,但如果下一个服务器名称较短,它不会完全覆盖。我已经尝试了我发现的各种建议,但没有工作,或者它们搞砸了我的进度条。
我正在运行osx,但也可能在各种Linux发行版上使用它。
任何建议都会很棒!
#!/bin/bash
HOSTS=("atlanta.voip.ms" "atlanta2.voip.ms" "chicago.voip.ms" "chicago2.voip.ms" "chicago3.voip.ms" "chicago4.voip.ms" "dallas.voip.ms" "denver.voip.ms" "denver2.voip.ms" "houston.voip.ms" "houstonnew1.voip.ms" "houstonnew2.voip.ms" "losangeles.voip.ms" "losangeles2.voip.ms" "newyork.voip.ms" "newyork2.voip.ms" "newyork3.voip.ms" "newyork4.voip.ms" "sanjose.voip.ms" "sanjose2.voip.ms" "seattle.voip.ms" "seattle2.voip.ms" "seattle3.voip.ms" "tampa.voip.ms" "tampanew1.voip.ms" "tampanew2.voip.ms" "washington.voip.ms" "washington2.voip.ms" "montreal.voip.ms" "montreal2.voip.ms" "montreal3.voip.ms" "montreal4.voip.ms" "toronto.voip.ms" "toronto2.voip.ms" "toronto3.voip.ms" "toronto4.voip.ms" "vancouver.voip.ms" "vancouver2.voip.ms" "amsterdam.voip.ms" "london.voip.ms" "melbourne.voip.ms" "paris.voip.ms")
Smallest="200000"
Server=""
tLen=${#HOSTS[@]}
# Slick Progress Bar
# Created by: Ian Brown (ijbrown@hotmail.com)
# Please share with me your modifications
# Functions
PUT(){ echo -en " 33[${1};${2}H";}
DRAW(){ echo -en " 33%";echo -en " 33(0";}
WRITE(){ echo -en " 33(B";}
HIDECURSOR(){ echo -en " 33[?25l";}
NORM(){ echo -en " 33[?12l 33[?25h";}
function showBar {
percDone=$(echo 'scale=2;'$1/$2*100 | bc)
halfDone=$(echo $percDone/2 | bc)
barLen=$(echo ${percDone%'.00'})
halfDone=`expr $halfDone + 6`
tput bold
PUT 7 28; printf "%4.4s " $barLen%
PUT 5 $halfDone; echo -e " 33[7m 33[0m"
tput sgr0
}
# Start Script
clear
HIDECURSOR
echo -e ""
echo -e ""
DRAW
echo -e " PLEASE WAIT WHILE SCRIPT IS IN PROGRESS"
echo -e " lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqk"
echo -e " x x"
echo -e " mqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj"
echo -e ""
echo -e ""
WRITE
for (( i=0; i<${tLen}; i++ ))
do
showBar $i ${tLen}
serl=${HOSTS[$i]}
seru=$(echo "$serl" | tr '[:lower:]' '[:upper:]')
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
#this line needs to overwrite completley
echo -ne "" 'r " TESTING:" $seru
Current1=` ping -c 4 -q -i .2 ${HOSTS[$i]} | grep avg | awk -F'/' '{print $5 }'`
Current=${Current1/./}
if [ "$Current" -lt "$Smallest" ]
then
Server=${HOSTS[$i]}
Smallest=$Current
fi
done
clear
Smallestd=$(echo "$Smallest" | sed 's/...$/.&/')
echo "Fastest Server = $Server @ $Smallestd ms"
下面是在显示仪表期间以及在完成后恢复光标时控制清除到行尾的示例。我已经反转了循环,以显示仪表从 100%(满)到 1% 自行清理
:#!/bin/bash
## string of characters for meter (60 - good for 120 char width)
str='▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒'
tput civis # make cursor invisible
for i in `seq 100 -1 1`; do # for 1 to 100, save cursor, restore, output, restore
printf " 33[s 33[u Progress: %s %3d %% 33[u" "${str:0:$(((i+1)/2))}" "$i"
sleep 0.1 # small delay
done
sleep 0.5
printf " 33[K" # clear to end-of-line
tput cnorm # restore cursor to normal
exit 0
注意:仪表显示期间的光标控制和清除由 ANSI 转义提供,用于save cursor position
和restore cursor position
。完成后,clear to end of line
用于清理。 tput
用于控制光标可见性,但也可用于保存、恢复和清除到行尾。
你可以用空格填充你的,这样你总是写相同数量的字符。 这样,您将用空格覆盖之前多余的字符。 例如,你可以像这样
echo -en "r"; echo -n $(printf " TESTING: %-40s" $seru)
感谢David C. Rankin,我有一个简单的工作答案
echo -ne "" 'r' " TESTING:" $seru ' 33[K'