状态栏在途中停止工作

时间:2020-03-05 21:02:08

标签: linux bash shell syntax terminal

我正在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

1 个答案:

答案 0 :(得分:1)

${var:a:b}从位置a的{​​strong> 个字符中提取一个子字符串。

我想您在所有情况下都希望b

${BAR:0:$i}

echo -ne "\r ${RED} ${BAR:0:$i}" echo -ne "\r ${YELLOW} ${BAR:0:$i}" 恰好起作用,因为${BAR:21:$i}足够长。