Skip to content

Instantly share code, notes, and snippets.

@bf4 bf4/clock
Created Apr 3, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.