Skip to content

Instantly share code, notes, and snippets.

@cirias
Created December 14, 2017 07:47
Show Gist options
  • Save cirias/e86e6425e98b20ff93b39af013d12b70 to your computer and use it in GitHub Desktop.
Save cirias/e86e6425e98b20ff93b39af013d12b70 to your computer and use it in GitHub Desktop.
watch and execute
#!/bin/bash
# monitor STDIN
# run command when interval of lines come in smaller than sepecific duration
DURATION=2 # in seconds
LINES_COUNT=1
COMMAND="echo Aha!"
SECONDS=$DURATION
COUNTER=$LINES_COUNT
while true; do
read line
if (( SECONDS >= DURATION )); then
SECONDS=0
COUNTER=$LINES_COUNT
continue
fi
COUNTER=$((COUNTER-1))
if (( COUNTER <= 0 )); then
eval $COMMAND
SECONDS=$DURATION
COUNTER=$LINES_COUNT
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment