aboutsummaryrefslogtreecommitdiff
path: root/bin/maybe
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-08-09 11:19:29 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-08-09 11:19:29 +1200
commitc80b70a994983f3ac56e73407f2a534993843c0f (patch)
tree8e1921d1666b65b04e23914359f47815e4813866 /bin/maybe
parentAdd issue grizzling about pandoc(1) (diff)
downloaddotfiles-c80b70a994983f3ac56e73407f2a534993843c0f.tar.gz
dotfiles-c80b70a994983f3ac56e73407f2a534993843c0f.zip
Rename myb(1) back to maybe(1)
Changed my mind again
Diffstat (limited to 'bin/maybe')
-rwxr-xr-xbin/maybe24
1 files changed, 24 insertions, 0 deletions
diff --git a/bin/maybe b/bin/maybe
new file mode 100755
index 00000000..86785a45
--- /dev/null
+++ b/bin/maybe
@@ -0,0 +1,24 @@
+#!/bin/sh
+# Exit with success or failure with a given probability
+self=maybe
+
+# 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
+
+# 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
+seed=$(rnds)
+test "$(rndi 1 "$den" "$seed")" -le "$num"