Skip to content

Instantly share code, notes, and snippets.

@bf4
Created April 3, 2019 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/c114fe1330a2defaaed1e43989f9dbb0 to your computer and use it in GitHub Desktop.
Save bf4/c114fe1330a2defaaed1e43989f9dbb0 to your computer and use it in GitHub Desktop.
Script to run only in the specified minute range
#!/usr/bin/env bash
# Created by Benjamin Fleischer 2019
# Distributed under the MIT license
#
# Usage:
# MINUTE_RANGES="0...10 30...40" ./clock echo 'we done it'
# DEBUG=true MINUTE_RANGES="0...10 30...40" ./clock echo 'we done it'
main() {
local ranges
if [ ${#MINUTE_RANGES[@]} -eq 0 ]; then
echo "MINUTE_RANGES not set. See USAGE. Not Executing: $@" >&2
exit 1
else
ranges=( $MINUTE_RANGES )
fi
local range_number
range_number=0
local number_of_ranges
number_of_ranges=${#ranges[@]}
local current_min="$(date +"%M")"
local ruby_condition
ruby_condition="min = '${current_min}'.to_i; exit ("
for range in ${ranges[*]} ; do
range_number=$(( range_number+1 ))
ruby_condition="${ruby_condition} (${range}).cover?(min)"
if [ $range_number -lt $number_of_ranges ]; then
ruby_condition="${ruby_condition} || "
fi
done
ruby_condition="${ruby_condition} )"
if [ ! -z "${DEBUG:-}" ]; then
echo "ruby_condition: ${ruby_condition}"
fi
if ruby -e "${ruby_condition}"; then
echo "ranges=[${ranges[*]}] current_min=${current_min} Executing: $@"
exec $@
else
echo "ranges=[${ranges[*]}] current_min=${current_min} NOT Executing: $@"
true
fi
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment