Skip to content

loop.sh

Timothée Mazzucotelli edited this page Oct 10, 2018 · 6 revisions

loop.sh

Control the loops within your scripts (pause/stop them).

Description

The loop.sh library provides functions to control loops within shells scripts. When used, it allows to control the execution of already running loops from whatever location or terminal. It is therefore possible to pause a script from another terminal without hitting Control-Z in the shell process running the script.

This execution control mechanism can even allow to control several loops and inner loops (nested loops) at the same time, or make different scripts dependents on each other.

Functions

loop_alive <NAME>

Check if the loop is alive (exists and not paused).

Arguments

  • NAME: The name of the loop to check.

Return codes

  • 0: The loop is alive.
  • 1: The loop is not alive.

loop_control <NAME>

Wait as long as the loop is paused, return 1 when it's dead.

Use this function like this: loop_control $NAME || break This function is a shortcut for: if loop_paused $NAME; then loop_wait $NAME elif loop_dead $NAME; then break fi

Arguments

  • NAME: The name of the loop to control.

loop_dead <NAME>

Check if the loop is dead.

Arguments

  • NAME: The name of the loop to check.

Return codes

  • 0: The loop is dead.
  • 1: The loop is not dead.

loop_exists <NAME>

Check if the loop exists.

Arguments

  • NAME: The name of the loop to check.

Return codes

  • 0: The loop exists.
  • 1: The loop does not exist.

loop_init <NAME>

Initialize a loop.

Arguments

  • NAME: The name of the loop to initialize.

Return codes

  • 0: The loop was correctly initialized.
  • 1: The loop was already initialized.

loop_pause <NAME>

Pause a loop.

Arguments

  • NAME: The name of the loop to pause.

Return codes

  • 0: The loop existed and was paused.
  • 1: The loop did not exist.

loop_paused <NAME>

Check if the loop is paused.

Arguments

  • NAME: The name of the loop to check.

Return codes

  • 0: The loop is paused.
  • 1: The loop is not paused.

loop_resume <NAME>

Resume a loop.

Arguments

  • NAME: The name of the loop to resume.

Return codes

  • 0: The loop existed and was resumed.
  • 1: The loop did not exist.

loop_stop <NAME>

Stop a loop.

Arguments

  • NAME: The name of the loop to stop.

Return codes

  • 0: The loop existed and was stopped.
  • 1: The loop did not exist.

loop_wait <NAME>

Wait as long as a loop is paused.

Arguments

  • NAME: The name of the loop to wait.

loop_list

List the currently existing loops.


loop <COMMAND> <NAME>

Main wrapper function accepting subcommands.

COMMAND can be the following:

  • alive: return True if the loop is alive, False otherwise.
  • control: shortcut for loop paused? wait. loop dead? break.
  • dead: return True if the loop is dead, False otherwise.
  • exists: return True if loop has been initialized, False otherwise.
  • init: init a new loop control and start it.
  • pause: pause the loop. It will wait until resumed or stopped.
  • resume: resume the loop.
  • stop: definitely stop the loop.
  • wait: wait as long as loop is paused.

Arguments

  • COMMAND: The subcommand to run.
  • NAME: The name of the loop on which to act.

Return codes

  • ?: The return code of the subcommand.
  • 1: When the subcommand is unknown.

Examples

  • In a script:

    #!/bin/bash
    source $(shellm-core-path)
    shellm-source shellm/loop
    
    loop init "script.loop"
    
    i=0
    while true; do
    
      loop control "script.loop" || break
    
      echo "$i"
      (( i++ ))
      sleep 1
    done
  • Then, from another terminal:

    $ loop pause "script.loop"
    $ loop resume "script.loop"
    $ loop stop "script.loop"

Wiki page generated with shellman.