我正在用bash编写计时器程序,我希望状态栏在完成时会更改颜色。进度栏的第一个(绿色)和第二个(黄色)三分之一按预期工作。最后三分之一(红色)显示不正确,但是计数器保持工作状态。我在语法上做错了什么?
#!/bin/sh
#USAGE:
#timer <minutes>
clear
#add colors
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
PURPLE='\033[0;35m'
LtGrn='\033[1;32m'
NC='\033[0m' # No Color
BAR='+###################+###################+###################' # this is full bar, e.g. 60 chars
echo -e "${PURPLE}======================================================================${NC}"
echo
echo -e " Setting Timer for: ${YELLOW} $1 ${NC} minutes"
echo -e " ${PURPLE} \o| ${RED} 25% ${YELLOW} 50% ${GREEN} 75% ${PURPLE} |o/ ${NC}"
for i in {1..60}; do
if [ $i -ge 41 ]
then
echo -ne "\r ${RED} ${BAR:41:$i}" # print $i chars of $BAR from 0 position
sleep $1 # wait 1/60th of total time between "frames"
fi
if [ $i -ge 21 ] && [ $i -lt 41 ]
then
echo -ne "\r ${YELLOW} ${BAR:21:$i}" # print $i chars of $BAR from 0 position
sleep $1 # wait 1/60th of total time between "frames"
fi
if [ $i -lt 21 ]
then
echo -ne "\r ${GREEN} ${BAR:0:$i}" # print $i chars of $BAR from 0 position
sleep $1 # wait 1/60th of total time between "frames"
fi
done
echo
echo
echo -e "${RED} Times up! Next task${NC}"
echo
${var:a:b}
提取子字符串从位置a
到位置b
。
我想你在所有情况下都想要${BAR:0:$i}
。
echo -ne "\r ${RED} ${BAR:0:$i}"
echo -ne "\r ${YELLOW} ${BAR:0:$i}"
[${BAR:21:$i}
刚好起作用,因为${#BAR}
足够长。