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?
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.
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?