aboutsummaryrefslogtreecommitdiff
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
parentChange maybe(1) to POSIX sh, rename myb(1) (diff)
downloaddotfiles-84d4cc96515d440fd03cf5149d4ed042c74c3d36.tar.gz
dotfiles-84d4cc96515d440fd03cf5149d4ed042c74c3d36.zip
Allow numerator and denominator in myb(1)
-rwxr-xr-xbin/myb27
-rw-r--r--man/man1/myb.113
2 files changed, 24 insertions, 16 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"
diff --git a/man/man1/myb.1 b/man/man1/myb.1
index ef9babc9..39753591 100644
--- a/man/man1/myb.1
+++ b/man/man1/myb.1
@@ -3,17 +3,24 @@
.B myb
\- possibly exit with success
.SH USAGE
-.B myb [DENOMINATOR]
+.B myb
+.br
+.B myb
+DENOMINATOR
+.br
+.B myb
+NUMERATOR DENOMINATOR
.SH DESCRIPTION
Like true(1) or false(1), but exits with success randomly with a given
probability. Good for using in tests. Exits with 2 rather than 1 on usage
errors.
.P
-The denominator defaults to 2 for a roughly equal chance of success or failure.
-rndi(1) is used for the randomness.
+The numerator default to 1 and the denominator to 2 for a roughly equal chance
+of success or failure. rndi(1) is used for the randomness.
.P
$ myb
$ myb 3
+ $ myb 2 5
.SH SEE ALSO
true(1), false(1), try(1), rndi(1)
.SH AUTHOR