Skip to content

Instantly share code, notes, and snippets.

@e0da
Created October 11, 2012 21:43
Show Gist options
  • Save e0da/3875691 to your computer and use it in GitHub Desktop.
Save e0da/3875691 to your computer and use it in GitHub Desktop.
watch like script
#!/bin/bash
#
# st_repeat HELP:
# st_repeat [-d <delay>] [-i] [-c <count>] [-k] <command>
# Repeat the given command.
#
# By default, this command will exit upon success, unless -i or -c are given.
#
# -d (default 2) specifies how long to wait between iterations
# -i will repeat the command indefinitely
# -c will repeat the command the specified number of times
# -k will clear the screen before each iteration
# ENDHELP
tt=/tmp/rep.$$
trap 'rm -f $tt;exit' 0
unset OPTIND;
delay=2;
on_success=1;
infinite=1;
count=-1;
clear=1;
while getopts ":d:sikc:" options; do
case $options in
d ) delay=${OPTARG};
;;
i ) infinite=1;
;;
c ) count=${OPTARG};
;;
k ) clear=0;
;;
* ) st_help st_repeat;
return 1;
;;
esac;
done;
[ ${infinite} -eq 1 ] && [ ${count} -gt -1 ] && \
echo "You can only give a count or specify infinity" && return 1
shift $((${OPTIND} - 1))
i=1;
while [ 1 ]; do
[ ${clear} -eq 1 ] && clear > $tt;
date +%T >> $tt
echo "" >> $tt
"$@" >> $tt
status=$?
cat $tt
# if on_success was specified and exit status was 0, break
[ ${infinite} -ne 1 ] && \
[ ${count} -lt 0 ] && \
[ ${on_success} -eq 1 ] && \
[ ${status} -eq 0 ] && break;
let i="${i} + 1";
# if a count was specified, break
[ ${infinite} -ne 1 ] && \
[ ${count} -gt -1 ] && \
[ ${i} -gt ${count} ] && break;
# sleep
sleep ${delay}
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment