计时器计数至零,然后显示负值

计时器计数至零,然后显示负值

在 shellcheck 中运行它,我得到:

line 20 while [ -1 ]; do

In TimerInTerminal.sh line 20:
while [ -1 ]; do
        ^-- SC2078: This expression is constant. Did you forget a $ somewhere?
line 36 for i in `seq 1 180`; # for i = 1:180 (i.e. 180 seconds)

In TimerInTerminal.sh line 36:
for i in `seq 1 180`; # for i = 1:180 (i.e. 180 seconds)
^-- SC2034: i appears unused. Verify it or export it.
         ^-- SC2006: Use $(..) instead of legacy `..`.

#!/bin/sh -x
# script to create timer in terminal
# Jason Atwood
# 2013/6/22
# displays negative numbers ?
# 31: [: 0: unexpected operator

# Start up
echo "starting timer script ..."
sleep 1 # seconds

# Get input from user
read -r "Timer for how many minutes?"  DURATION
DURATION=$(( DURATION*60 )) # convert minutes to seconds

# Get start time
START=$(date +%s)

# Infinite loop
while [ -1 ]; do
clear # Clear window

# Do math
NOW=$(date +%s)    # Get time now in seconds
DIF=$(( NOW-START ))    # Compute diff in seconds
ELAPSE=$(( DURATION-DIF ))    # Compute elapsed time in seconds
MINS=$(( ELAPSE/60 ))    # Convert to minutes... (dumps remainder from division)
SECS=$(( ELAPSE - (MINS*60) )) # ... and seconds

# Conditional
# unexpected operator
#if [ $MINS == 0 ]     # if mins = 0 and secs = 0 (i.e. if time expired)
#if [ $MINS -eq 0 ]
if [ $MINS -le 0 ]
then # Blink screen
for i in `seq 1 180`; # for i = 1:180 (i.e. 180 seconds)
do
clear # Flash on
setterm -term linux -back red -fore white # use setterm to change background color
echo "00:00 " # extra tabs for visibility

sleep 0.5

clear # Flash off
setterm -term linux -default # Clear setterm changes from above
echo "00:00" # (I.e. go back to white text on black background)
sleep 0.5
done # End for loop
break    # End script

else # Else, time is not expired
echo "$MINS:$SECS"    # Display time
sleep 1 # Sleep 1 second
fi    # End if
done    # End while loop

在此处输入图片描述

答案1

#!/bin/sh
#     TimerInTerminal.sh

# had to install banner sudo apt install sysvbanner
# script to create timer in terminal
# MadeInGermany

soundfile="/usr/share/sounds/My_Sounds/Electronic_Chime.wav"

if [ $# -eq 1 ]
then
    DURATION="$1"
else
    read -p "Timer for how many minutes?( for fractional use decimal notation , 0.5==30s, 1.25==75s etc) : "  DURATION
fi

DURATION=$(echo "$DURATION * 60 / 1" | bc) # lets us deal with fractional inputs

START=$(date +%s)   # only do this once (anchor's the time)

countdown () {
    NOW=$(date +%s)              # Get time now in seconds
    DIF=$((NOW - START))         # Compute diff in seconds
    ELAPSE=$((DURATION - DIF))   # Compute elapsed time in seconds
    MINS=$((ELAPSE / 60))        # Convert to minutes... (dumps remainder from division)
    SECS=$((ELAPSE - (MINS*60))) # ... and seconds
    banner "$MINS:$SECS"
    sleep $1
}

while true 
do
    clear
    
    countdown 0 # calc time remaining
    
    if [ $MINS -le 0 ]
    then
        # Blink screen

        while [ $SECS -gt 0 ]
        do
            
            clear # Flash on
            setterm -term linux -back red -fore white 
            countdown 0.5

            clear # Flash off
            setterm -term linux -default
            countdown 0.5

        done # End for loop
        
        setterm -term linux -default
        clear
        
        break   # time has expired lets get out of here
    
    else
        countdown 1 
    fi
done
echo; banner "Time is up."; echo ;
cvlc --play-and-exit "$soundfile" > /dev/null 2>&1

相关内容