-
Notifications
You must be signed in to change notification settings - Fork 0
loop.sh
Control the loops within your scripts (pause/stop them).
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.
Check if the loop is alive (exists and not paused).
-
NAME
: The name of the loop to check.
-
0
: The loop is alive. -
1
: The loop is not alive.
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
-
NAME
: The name of the loop to control.
Check if the loop is dead.
-
NAME
: The name of the loop to check.
-
0
: The loop is dead. -
1
: The loop is not dead.
Check if the loop exists.
-
NAME
: The name of the loop to check.
-
0
: The loop exists. -
1
: The loop does not exist.
Initialize a loop.
-
NAME
: The name of the loop to initialize.
-
0
: The loop was correctly initialized. -
1
: The loop was already initialized.
Pause a loop.
-
NAME
: The name of the loop to pause.
-
0
: The loop existed and was paused. -
1
: The loop did not exist.
Check if the loop is paused.
-
NAME
: The name of the loop to check.
-
0
: The loop is paused. -
1
: The loop is not paused.
Resume a loop.
-
NAME
: The name of the loop to resume.
-
0
: The loop existed and was resumed. -
1
: The loop did not exist.
Stop a loop.
-
NAME
: The name of the loop to stop.
-
0
: The loop existed and was stopped. -
1
: The loop did not exist.
Wait as long as a loop is paused.
-
NAME
: The name of the loop to wait.
List the currently existing loops.
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.
-
COMMAND
: The subcommand to run. -
NAME
: The name of the loop on which to act.
-
?
: The return code of the subcommand. -
1
: When the subcommand is unknown.
-
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.