0

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?

0

1 Answer 1

1

You wrote:

(( sec >= 60 && sec > 3600 )) && # [...]

but you probably meant:

(( sec >= 60 && sec < 3600 )) && # [...]

Output:

....[1 min 10 sec]
.[1 min 40 sec]
.[2 min 15 sec]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.