Countdown timer with seconds

I would like this to work with seconds as well.

#!/bin/bash
# 
 
if [ "$#" -lt "2" ] ; then 
    echo "Incorrect usage ! Example:" 
    echo './CountDown.sh -d  "Jun 10 2011 16:06"' 
    echo 'or' 
    echo './CountDown.sh -m  90' 
    exit 1 
fi 
 
now=`date +%s` 
 
if [ "$1" = "-d" ] ; then 
    until=`date -d "$2" +%s` 
    sec_rem=`expr $until - $now` 
    echo "-d" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi 
fi 
 
if [ "$1" = "-m" ] ; then 
    until=`expr 60 \* $2` 
    until=`expr $until + $now` 
    sec_rem=`expr $until - $now` 
    echo "-m" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi 
fi 
 
_R=0
_C=7
tmp=0
percent=0
total_time=0
col=`tput cols`
col=$[ $col -5 ]

while [ $sec_rem -gt 0 ]; do 
    clear 
    date 
    let sec_rem=$sec_rem-1 
    interval=$sec_rem 
    seconds=`expr $interval % 60` 
    interval=`expr $interval - $seconds` 
    minutes=`expr $interval % 3600 / 60` 
    interval=`expr $interval - $minutes` 
    hours=`expr $interval % 86400 / 3600` 
    interval=`expr $interval - $hours` 
    days=`expr $interval % 604800 / 86400` 
    interval=`expr $interval - $hours` 
    weeks=`expr $interval / 604800` 
    echo "----------------------------" 
    echo "Seconds: " $seconds 
    echo "Minutes: " $minutes 
    echo "Hours:   " $hours 
    echo "Days:    " $days 
    echo "Weeks:   " $weeks 

    echo -n "["

    progress=$[$progress + 1]
    if [ $total_time -lt 1 ] ; then
        total_time=$[$hours * 3600 + $minutes * 60 + $seconds]
    fi
    
    printf -v f "%$(echo $_R)s>" ; printf "%s\n" "${f// /=}"
    _C=7
    tput cup 7 $col

    tmp=$percent
    percent=$[$progress * 100 / $total_time]
    printf "]%d%%" $percent
    change=$[$percent - $tmp]

    _R=$[ $col * $percent / 100 ]

    sleep 1
done
printf "\n"

#!/bin/bash
# 
 
if [ "$#" -lt "2" ] ; then 
    echo "Incorrect usage ! Example:" 
    echo './CountDown.sh -d  "Jun 10 2011 16:06"' 
    echo 'or' 
    echo './CountDown.sh -m  90' 
    echo 'or'
    echo './CoundDown.sh -s 5'
    exit 1 
fi 
 
now=`date +%s` 
 
if [ "$1" = "-d" ] ; then 
    until=`date -d "$2" +%s` 
    sec_rem=`expr $until - $now` 
    echo "-d" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi 
fi 
 
if [ "$1" = "-m" ] ; then 
    until=`expr 60 \* $2` 
    until=`expr $until + $now` 
    sec_rem=`expr $until - $now` 
    echo "-m" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi
if [ "$1" = "-s" ] ; then 
    until=`expr \* $2` 
    until=`expr $until + $now` 
    sec_rem=`expr $until - $now` 
    echo "-s" 
    if [ $sec_rem -lt 1 ]; then 
        echo "$2 is already history !" 
    fi     
     
fi 
 
_R=0
_C=7
tmp=0
percent=0
total_time=0
col=`tput cols`
col=$[ $col -5 ]

while [ $sec_rem -gt 0 ]; do 
    clear 
    date 
    let sec_rem=$sec_rem-1 
    interval=$sec_rem 
    seconds=`expr $interval % 60` 
    interval=`expr $interval - $seconds` 
    minutes=`expr $interval % 3600 / 60` 
    interval=`expr $interval - $minutes` 
    hours=`expr $interval % 86400 / 3600` 
    interval=`expr $interval - $hours` 
    days=`expr $interval % 604800 / 86400` 
    interval=`expr $interval - $hours` 
    weeks=`expr $interval / 604800` 
    echo "----------------------------" 
    echo "Seconds: " $seconds 
    echo "Minutes: " $minutes 
    echo "Hours:   " $hours 
    echo "Days:    " $days 
    echo "Weeks:   " $weeks 

    echo -n "["

    progress=$[$progress + 1]
    if [ $total_time -lt 1 ] ; then
        total_time=$[$hours * 3600 + $minutes * 60 + $seconds]
    fi
    
    printf -v f "%$(echo $_R)s>" ; printf "%s\n" "${f// /=}"
    _C=7
    tput cup 7 $col

    tmp=$percent
    percent=$[$progress * 100 / $total_time]
    printf "]%d%%" $percent
    change=$[$percent - $tmp]

    _R=$[ $col * $percent / 100 ]

    sleep 1
done
printf "\n"
test.sh -s 9
 /home/andy/bin/test.sh: line 95: syntax error: unexpected end of file
test.sh -s 9  /home/andy/bin/test.sh: line 95: syntax error: unexpected end of file

I feel that the syntax error is kind of vague?

Count the "fi" s.

Added fi.

if [ "$1" = "-m" ] ; then
     until=`expr 60 \* $2`
      until=`expr $until + $now`
      sec_rem=`expr $until - $now`
      echo "-m"
      if [ $sec_rem -lt 1 ]; then
          echo "$2 is already history !"
      fi
  fi
andy@7_~/Downloads$ test.sh -s 20
expr: syntax error
-s
20 is already history !

Your sparse "specifications" and problem "descriptions" nor your sloppy and careless data posting, you frequently having to correct yourself, urging people in here to tidy up for you, don't really help find errors and create solutions for you.

That said, run the script with the shell's -x ( --xtrace ) option set and you'll immediately spot the error.

Your cutdowns add nothing.

If it bothers you so much, I recommend not responding.

I am not perfect and neither are you.

The above diagnostic message tells you that one of your 15 or so expr statements contains a syntax error.

One might guess that the statement in your code:

    until=`expr \* $2`

is likely to be one of your problems since multiplication is a binary operator and you have only supplied one operand.

You are correct; none of us are perfect. But, you should be familiar with your code. You should immediately know where to look for expr statements that are used just before you print out the string -s (which your code prints just after the diagnostic message). And, if you had followed RudiC's advice, it would have shown you exactly which of your expr invocations was generating the diagnostic. (Maybe you should have thanked him for the advice instead of telling him that you no longer want his help. You might consider that other people will read your comments and decide they no longer want to help you either.)

3 Likes