Bash - countdown timer

Hello, I have another problem with my script - I would like to have a countdown timer visible on the screen, and at the same time, I want te be able to do something else. And when the time runs out, I need to know about that inside the script somehow and do some action. I guess that would require 2 threads to achieve that but maybe there is another solution in bash?

Maybe there is some equivalent to fork() - and to give both processes different task to do?

Thanks in advance.

Hi.

If you have only one window, then you could use screen:

       Screen is a full-screen window manager that multiplexes a physical
       terminal between several processes (typically interactive shells).
       Each virtual terminal provides the functions of a DEC VT100 terminal ...

-- excerpt from man screen

Screen can split the current window into parts, see the split section.

It may take some time to get used to doing things with screen, but depending on your circumstances, it may be easier than other approaches.

Best wishes ... cheers, drl

Thank you, but I don't have much time for learning how the screen works, so I would be glad to find some other way. Here is a test srcipt that I came up with so far:

#!/bin/bash

function countdown()
{
        local OLD_IFS="${IFS}"
        IFS=":"
        local ARR=( $1 )
        local SECONDS=$((  (ARR[0] * 60 * 60) + (ARR[1] * 60) + ARR[2]  ))
        local START=$(date +%s)
        local END=$((START + SECONDS))
        local CUR=$START

        while [[ $CUR -lt $END ]]
        do
                CUR=$(date +%s)
                LEFT=$((END-CUR))
                printf "\r%02d:%02d:%02d" \
                        $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

                sleep 1
        done
        IFS="${OLD_IFS}"
        echo "        "
}

first()
{
  echo "before fork"
  printf "\033c"
}

parent()
{
  while [ 1 ]; do
  echo -ne "\r\e[4H"
  echo -n "parent"
  done
}

child()
{
echo -ne "\r\e[3H"
countdown "00:07:55"
}

first; parent & child

I would like, for example, the child process to print the time in the third line of the terminal, and the parent - just write "parent" multiple times in 4th line, in the same place, by overwriting the previous one. But both processes seem to be writing in the same line - can it be fixed somehow?

Hi.

I'd rather a program handle all the escapes, special sequences, etc. Here's an example that writes in a few places in my display:

#!/usr/bin/env bash

# @(#) s1	Demonstrate tput row column addressing, 0 0 upper left.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }

SLEEP=${1-10}

COLS=$( tput cols )
ROWS=$( tput lines )
tput init
tput clear

# Put in line (row) numbers.
for (( row=0 ; row < $ROWS ; row++ ))
do
  tput cup $row 0
  echo -n $row
done

tput cup 0 10
echo -n " This is the top row."

tput cup 3 4
echo -n "Hello, world (one)."
echo "Hello, world (two)."
echo "Hello, world (three)."

tput cup 5 4
echo -n "Midday, world (one)."
echo "Midday, world (two)."
echo "Midday, world (three)."

tput cup 8 4
echo -n "Goodbye, world (one)."
echo "Goodbye, world (two)."
echo "Goodbye, world (three)."

tput cup 10 30
echo -n "Sleep, clear, reset ..."
sleep $SLEEP
tput clear
tput reset

C=$HOME/bin/context && [ -f $C ] && $C tput
pe

exit 0

which will produce output in several lines on the screen.

See man tput for details ... cheers, drl