aboutsummaryrefslogtreecommitdiff
path: root/bin/myb
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-03 16:44:09 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-03 16:44:09 +1200
commit84d4cc96515d440fd03cf5149d4ed042c74c3d36 (patch)
tree2aedb661e8d1d726199618ec4ae0b3aa42c6d336 /bin/myb
parentChange maybe(1) to POSIX sh, rename myb(1) (diff)
downloaddotfiles-84d4cc96515d440fd03cf5149d4ed042c74c3d36.tar.gz
dotfiles-84d4cc96515d440fd03cf5149d4ed042c74c3d36.zip
Allow numerator and denominator in myb(1)
Diffstat (limited to 'bin/myb')
-rwxr-xr-xbin/myb27
1 files changed, 14 insertions, 13 deletions
diff --git a/bin/myb b/bin/myb
index 57a37ad5..11827fdd 100755
--- a/bin/myb
+++ b/bin/myb
@@ -2,21 +2,22 @@
# Exit with success or failure with a given probability
self=myb
-# No more than one argument
-if [ "$#" -gt 1 ] ; then
- printf >&2 '%s: Unexpected arguments\n' "$self"
- exit 2
-fi
-
-# Sole accepted argument is denominator of the probability fraction, e.g. 3 is
-# a probability of 1/3; defaults to 2
-denom=${1:-2}
+# Figure out numerator and denominator from arguments
+case $# in
+ 0) num=1 den=2 ;;
+ 1) num=1 den=$1 ;;
+ 2) num=$1 den=$2 ;;
+ *)
+ printf >&2 '%s: Unexpected arguments\n' "$self"
+ exit 2
+ ;;
+esac
-# Denominator must be positive integer (it can be one)
-if [ "$((denom > 0))" -ne 1 ] ; then
- printf >&2 '%s: Illegal denominator %s\n' "$self" "$denom"
+# Numerator must be zero or greater, denominator must be 1 or greater
+if [ "$((num >= 0 || den >= 1))" -ne 1 ] ; then
+ printf >&2 '%s: Illegal numerator/denominator %s\n' "$self"
exit 2
fi
# Perform the test; that's our exit value
-test "$(rndi 1 "$denom")" -eq 1
+test "$(rndi 1 "$den")" -le "$num"