aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown2
-rwxr-xr-xbin/maybe69
-rwxr-xr-xbin/myb22
-rw-r--r--man/man1/maybe.124
-rw-r--r--man/man1/myb.120
-rw-r--r--man/man1/try.14
6 files changed, 45 insertions, 96 deletions
diff --git a/README.markdown b/README.markdown
index ad4f78d1..afd005d0 100644
--- a/README.markdown
+++ b/README.markdown
@@ -338,7 +338,7 @@ Installed by the `install-bin` target:
repository
* `jfc(1)` adds and commits lazily to a Git repository.
* `jfcd(1)` watches a directory for changes and runs `jfc(1)` if it sees any.
-* `maybe(1)` is like `true(1)` or `false(1)`; given a probability of success,
+* `myb(1)` is like `true(1)` or `false(1)`; given a probability of success,
it exits with success or failure. Good for quick tests.
* `mkcp(1)` creates a directory and copies preceding arguments into it.
* `mkmv(1)` creates a directory and moves preceding arguments into it.
diff --git a/bin/maybe b/bin/maybe
deleted file mode 100755
index d017966a..00000000
--- a/bin/maybe
+++ /dev/null
@@ -1,69 +0,0 @@
-#!/usr/bin/env bash
-# Exit with success or failure with a given probability
-self=maybe
-
-# Print usage information
-usage() {
- printf '%s: usage: %s [-hv] [-d DENOMINATOR]\n' \
- "$self" "$self"
-}
-
-# Flag for whether to print diagnostics to stdout or not
-declare -i verbose
-verbose=0
-
-# Denominator of the probability fraction, e.g. 3 is a probability of 1/3;
-# defaults to 2
-declare -i denom
-denom=2
-
-# Process options
-while getopts 'hvd:' opt ; do
- case $opt in
-
- # -h: Print help
- h)
- usage
- exit 0
- ;;
-
- # -v: Print diagnostics to stdout
- v)
- verbose=1
- ;;
-
- # -d: Set the denominator of the probability fraction
- d)
- denom=$OPTARG
- ;;
-
- # Unknown option
- \?)
- usage >&2
- exit 2
- ;;
- esac
-done
-shift "$((OPTIND-1))"
-
-# If there are any non-option arguments or our denominator isn't a positive
-# integer, we're being abused and we won't put up with it
-if (($#)) || ! ((denom > 0)) ; then
- usage >&2
- exit 2
-fi
-
-# If verbose, report the probability of the test
-((verbose)) && printf '%s: Testing with probability 1/%u ... \n' \
- "$self" "$denom"
-
-# Perform the test and print/exit appropriately
-if ((RANDOM < 32767/denom)) ; then
- ((verbose)) && printf '%s: %s\n' \
- "$self" 'Success!'
- exit 0
-else
- ((verbose)) && printf '%s: %s\n' \
- "$self" 'Failure!'
- exit 1
-fi
diff --git a/bin/myb b/bin/myb
new file mode 100755
index 00000000..57a37ad5
--- /dev/null
+++ b/bin/myb
@@ -0,0 +1,22 @@
+#!/bin/sh
+# 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}
+
+# Denominator must be positive integer (it can be one)
+if [ "$((denom > 0))" -ne 1 ] ; then
+ printf >&2 '%s: Illegal denominator %s\n' "$self" "$denom"
+ exit 2
+fi
+
+# Perform the test; that's our exit value
+test "$(rndi 1 "$denom")" -eq 1
diff --git a/man/man1/maybe.1 b/man/man1/maybe.1
deleted file mode 100644
index 9fd90c8b..00000000
--- a/man/man1/maybe.1
+++ /dev/null
@@ -1,24 +0,0 @@
-.TH MAYBE 1 "February 2016" "Manual page for maybe"
-.SH NAME
-.B maybe
-\- possibly exit with success
-.SH USAGE
-.B maybe [-hv] [-d 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
-This just uses integer division with Bash's special RANDOM variable, so it's
-far from scientific precision.
-.P
-Option -h gives help, option -v turns on verbose output, option -d specifies
-the denominator for the probability; defaults to 2 (i.e. roughly equal chance
-of success or failure).
-.P
- $ maybe
- $ maybe -v -d3
-.SH SEE ALSO
-true(1), false(1), try(1)
-.SH AUTHOR
-Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/myb.1 b/man/man1/myb.1
new file mode 100644
index 00000000..ef9babc9
--- /dev/null
+++ b/man/man1/myb.1
@@ -0,0 +1,20 @@
+.TH MYB 1 "August 2016" "Manual page for myb"
+.SH NAME
+.B myb
+\- possibly exit with success
+.SH USAGE
+.B myb [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.
+.P
+ $ myb
+ $ myb 3
+.SH SEE ALSO
+true(1), false(1), try(1), rndi(1)
+.SH AUTHOR
+Tom Ryder <tom@sanctum.geek.nz>
diff --git a/man/man1/try.1 b/man/man1/try.1
index d3dcd1de..15182436 100644
--- a/man/man1/try.1
+++ b/man/man1/try.1
@@ -14,8 +14,8 @@ of attempts; defaults to 3. Options may be terminated with --. The remaining
arguments are the command to run.
.P
$ try gms
- $ try -v -n3 maybe
+ $ try -v -n3 myb
.SH SEE ALSO
-maybe(1)
+myb(1)
.SH AUTHOR
Tom Ryder <tom@sanctum.geek.nz>