💾 Archived View for rawtext.club › ~s0kx › pure-sh-bible › arithmetic.gmi captured on 2024-02-05 at 11:26:32. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

Arithmetic

Ternary Tests

# Set the value of var to var2 if var2 is greater than var.
# 'var2 > var': Condition to test.
# '? var2': If the test succeeds.
# ': var': If the test fails.
var=$((var2 > var ? var2 : var))

Check if a number is a float

is_float() {
    # Usage: is_float "number"

    # The test checks to see that the input contains
    # a '.'. This filters out whole numbers.
    [ -z "${1##*.*}" ] &&
        printf %f "$1" >/dev/null 2>&1
}

Example Usage:

$ is_float 1 && echo true
$

$ is_float 1.1 && echo true
$ true

Check if a number is an integer

is_int() {
    # usage: is_int "number"
    printf %d "$1" >/dev/null 2>&1
}

Example Usage:

$ is_int 1 && echo true
$ true

$ is_int 1.1 && echo true
$