Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

break loop with key, still don't exit the program + code



How can I prematurely break from a loop, or
a deliberately infinite loop for that matter,
while still don't exit the program?

I.e.,

#! /bin/zsh

while (true); do
    # do computations  
done
# when interrupted by the user,
# do a summary of the computations
# and output

# only then, exit program

The particular use case is yanked last.
Feel free to comment on the code as well, of
course!

The purpose is I stretch for ~1h every day
(e.g. today 1h 3m 55s) and use the below
functions to signal when too change position.
Only sometimes, I don't do the entire program,
say I stretch for ~30m or whatever instead.
With the output from the 'stretch' program,
when interrupted prematurely, I can do

$ echo $(s2hms $(( 28*65 )) )
30m 20s

and write it down it in my flipboard. That way,
digits won't lie :)

Only, I'd like to have this automated so
whenever 'stretch' is interrupted, last thing
it'll do is make the computation and output it.

Thanks in advance :)

#! /bin/zsh

# https://dataswamp.org/~incal/conf/.zsh/stretch
play-stretch-sound () {
    local sound=~/bo/up.mp3

    if [[ -f $sound ]]; then
        omx-play-whatever $OMX_SND_MOD $sound
    else
        echo "Error: Can't find sound file: $sound" >&2
    fi
}

stretch () {
    local init_time=19

    local s_time=65
    local stretches=70

    local total_s=$(( $s_time * $stretches ))
    local total_time="$(s2hms ${total_s})"

    echo "Total time: $total_time (${stretches}*${s_time}s)"
    play-stretch-sound

    local current=0
    sleep $init_time
    echo -n "current:"
    repeat $stretches {
            current=$(( $current + 1 ))
            echo -n " $current"

            play-stretch-sound
            sleep $s_time
    }
    repeat 2 play-stretch-sound
}
alias st=stretch

# https://dataswamp.org/~incal/conf/.zsh/time
s2hms () {
    local secs=$1

    local time_string
    time_string=$(units -t $secs\s hms)

    local -a data
    data=("${(@s/;/)time_string}")

    local h=$data[1]
    local m=$data[2]
    local s=$data[3]

    local out
    [[ $h > 0 ]] &&  out="${h}h "
    [[ $m > 0 ]] && out+="${m}m "
    [[ $s > 0 ]] && out+="${s}s"
    echo $out
}
alias hms=s2hms

-- 
underground experts united
http://user.it.uu.se/~embe8573



Messages sorted by: Reverse Date, Date, Thread, Author