1

Normally these would go in one's .bashrc file to be used as a stopwatch, but they are sort of inconvienent to use and recall with the several different commands for the different units of time.

function time_seconds()
{
  echo "Counting to $1 seconds"
  time_func $1
}

function time_minutes()
{
  echo "Counting to $1 minutes"
  time_func $1*60
}

function time_hours()
{
  echo "Counting to $1 hours"
  time_func $1*60*60
}

function time_flexible()  # accepts flexible input hh:mm:ss (does not work)
{
    echo "Counting to $1"
    secs=$(time2seconds $1)
    time_func $secs
}

enter image description here

I would like to convert all these functions, commands into one function, one command. I think a lot of people would find this almost as useful as the sleep command... well, not quite... but still very convenient to have. Speaking of the sleep command, it works as follows, example:

sleep 5h 22m 44s

I would like to turn these commands into a one stopwatch command in which accepts a input values exactly like the sleep command, example:

stopwatch 5h 22m 44s

How would I go about accomplishing this?

p.s. feel free to modify the stopwatch display output if you see your changes making this more universally accepted as simpler and easier.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Anonymous
  • 503

1 Answers1

3

Assuming you get the duration in plain seconds or as positive integers suffixed by h, m and/or s, the following will calculate the duration as seconds (but will not verify that the provided arguments, sans suffix, are actually positive integers, nor is it very careful about making sure the suffixes are correctly specified):

to_seconds () {
    seconds=0

    for ts do
        case $ts in
            *h) seconds=$(( seconds + 60*60*"${ts%h}" )) ;;
            *m) seconds=$(( seconds +    60*"${ts%m}" )) ;;
             *) seconds=$(( seconds +       "${ts%s}" )) ;;
        esac
    done

    printf '%s\n' "$seconds"
}

The loop in the function will loop over the given arguments.

The ${ts%h} parameter substitution would delete a trailing h from the end of the $ts value, if there is one there.

If one used 5m 5m 5m as arguments, the number of seconds for 15 minutes would be computed.

You should be able to use this in your code, possibly not in this exact form (as a separate function) but as part of whatever code you are currently writing.

Testing:

$ to_seconds 5h 22m 44s
19364
$ to_seconds 5h 22m 44
19364
$ to_seconds 44
44
$ to_seconds 10
10
$ to_seconds 10m
600
$ to_seconds 10h
36000
$ to_seconds 2i
bash: seconds +       2i: value too great for base (error token is "2i")

To verify that a given number is an integer, see

Kusalananda
  • 333,661
  • This doesn't count down though, and only does seconds? – Anonymous Apr 04 '19 at 23:18
  • @AnonymousUser The issue you had was about unifying three different functions into one. You showed a time_flexible() function that you said did not work. I have given you a way of calculating seconds from hours, minutes and seconds (given on the format that you prescribed). I'm assuming that you would either call this (with your own modifications added), or incorporate the already existing counting-down-code into it. I'm not going to write code that you already have and that you seem to have no issue with. – Kusalananda Apr 05 '19 at 05:32