I was writing a code to print dots for every 10 seconds of sleep. Also, at every sleep session end, the sleep interval should increase by 5. And if remaining sleep sessions is less than half of total sleep sessions (i.e. 6) then the script should display the elapsed time.
Here is the code,
#!/bin/bash
current_sleep_sess=0
total_sleep_sess=6
rem_sleep_sess=6
sleep_interval=10
SECONDS=0
elapsed_time()
{
sec=$1
(( sec < 60 )) && echo -e "[Elapsed Time : $sec sec]\c"
(( sec >= 60 && sec > 3600 )) && echo -e "[$(( sec / 60 )) min $(( sec % 60 )) sec]\c"
(( sec >= 3600 )) && echo -e "[$(( sec / 3600 )) hrs $(( (sec % 3600) / 60)) min $(( (sec % 3600) % 60 )) sec]\c"
}
until (( rem_sleep_sess == 0 ))
do
(( current_sleep_sess=$current_sleep_sess+1 ))
sleep $sleep_interval
echo -e ".\c"
if (( rem_sleep_sess <= $((total_sleep_sess / 2)) ))
then
elapsed_time $SECONDS
echo
fi
(( sleep_interval=$sleep_interval+5 ))
(( rem_sleep_sess=$total_sleep_sess-$current_sleep_sess ))
done
The problem is that when I use elapsed_time $SECONDS without the if clause, then the function executes, but when I use it within the if clause, then the function gets skipped. Is there something, some syntax rule that I am missing?