aboutsummaryrefslogtreecommitdiff
path: root/bin/maybe.sh
blob: 8a4f246eb879a7498f852273ac9db6ee99bd65fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 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" "$num"
    exit 2
fi

# Perform the test; that's our exit value
test "$(rndi 1 "$den")" -le "$num"