1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Determine whether a value matches a crontab-like numeric pattern (echo 1 for true and 0 for false)

function is_a_factor {
	quotient=`echo "$2 $1" | awk '{print $1/$2}'`
	
	if [[ $quotient == *.* ]]; then
		echo 0
	else
		echo 1
	fi
}

function matches_pattern {

	value="$1"
	pattern="$2"
	pattern=`echo "$pattern" | tr -d [:space:]`
	patterns=`echo "$pattern" | tr ',' '\n'`
	
	# Check for the existence of any asterisks
	asterisk_count=`echo "$patterns" | grep "^\*$" | wc -l`
	
	if [[ $asterisk_count -gt 0 ]]; then
		echo 1
		exit
	fi
	
	# Find all patterns that are either integers or ranges (e.g. 3-6) and format them for use in a case statement (e.g. [3-6] ) echo 1; exit ;;) 
	case_patterns=`echo "$patterns" | grep "^[0-9-]\{1,\}$" | sed 's/\([0-9]\{1,\}-[0-9]\{1,\}\)/[\1]/g' | sed 's/\($\)/\1 \) echo 1; exit ;;/'`
	
	# Check whether the value matches any of the integer or range patterns
	eval "
		case ${value} in
			${case_patterns}
		esac
	"
	
	# Find all asterisk/integer patterns (e.g. */2) and remove the "*/" to leave jus the integer
	divisors=`echo "$patterns" | grep "^\*\/[0-9]\{1,\}$" | sed 's/[^0-9]\{1,\}//g'`
	
	echo "$divisors" > divisors.txt
	
	# Check whether the value is a factor of any of the divisors
	while read divisor; do
		if [ $(is_a_factor $divisor $value) -eq 1 ]; then
			echo 1
			rm divisors.txt
			exit
		fi
	done < divisors.txt
	
	rm divisors.txt
	
	echo 0
	exit

}

value=7

pattern="3,19,*/3,5-8,*/14"

matches_pattern "$value" "$pattern"