shell倒计时下班时间
阅读原文时间:2023年07月15日阅读:1

#!/bin/sh

offWorkTime="19:00:00"
offWorkHour=${offWorkTime::}
offWorkMinute=${offWorkTime::}
offWorkSecond=${offWorkTime::}

currentTime=`TZ='Asia/Hong_Kong' date +%H:%M:%S`
currentHour=${currentTime::}
currentMinute=${currentTime::}
currentSecond=${currentTime::}

second=$((offWorkSecond - currentSecond))
if [[ $second -lt 0 ]]
then
  second=$((second + 60))
  offWorkMinute=$((offWorkMinute - 1))
  if [[ $offWorkMinute -lt 0 ]]
  then
    offWorkMinute=$((offWorkMinute + 60))
    offWorkHour=$((offWorkHour - 1))
  fi
fi

minute=$((offWorkMinute - currentMinute))
if [[ $minute -lt 0 ]]
then
  minute=$((minute + 60))
  offWorkHour=$((offWorkHour - 1))
fi

hour=$((offWorkHour - currentHour))

while true
do
  second=$((second - 1))
  if [[ $second -lt 0 ]]
  then
    second=$((second + 60))
    minute=$((minute - 1))
    if [[ $minute -lt 0 ]]
    then
      minute=$((minute + 60))
      hour=$((hour - 1))
    fi
  fi

  if [[ $second -lt 10 ]]
  then
    secondStr="0$second"
  else
    secondStr="$second"
  fi

  if [[ $minute -lt 10 ]]
  then
    minuteStr="0$minute"
  else
    minuteStr="$minute"
  fi

  if [[ $hour -lt 10 ]]
  then
    hourStr="0$hour"
  else
    hourStr="$hour"
  fi

  tput cup 0 0
  tput civis
  echo $(tput setaf 1; tput setab 6; tput bold)"offworkTime: $hourStr:$minuteStr:$secondStr"$(tput sgr0)

  if [[ $hour -eq 0 && $minute -eq 0 && $second -eq 0 ]]
  then
    break
  fi

  sleep 1s
done