#!/usr/bin/env bash # lib_int.sh - the integer rule for knobs (Issue #26), on its own so scripts # that run without lib.sh (diff_collect.sh and loop_lock.sh, which the Python # runner and the tests source alone) read their knobs the same way. lib.sh # sources it. Functions only: sourcing it has no other effect. # mam_int # # Issue #26: the one integer rule for knobs that reach bash arithmetic (the # Python twin is lib_py/loop/runner.py::_positive_int). Surrounding blanks are # allowed, then digits only, read as DECIMAL (bash would read 0700 as octal # 448 and fail on 0900), and min <= n <= 999999999. Prints n and returns 0, or # returns 1. The per-knob minimum lives in tests/test_issue26_ints.py. mam_int() { # Spelled-out ASCII sets, not [[:space:]]/[0-9]: those follow the locale, and # the runner's _positive_int takes ASCII only (review R2: U+2003 30 U+2003 # was 30 here under en_US.UTF-8 and invalid there). local _ws=$' \t\n\r\v\f' _re _re="^[${_ws}]*([0123456789]+)[${_ws}]*\$" [[ "${1-}" =~ $_re ]] || return 1 local d="${BASH_REMATCH[1]}" d="${d#"${d%%[!0]*}"}" [ ${#d} -le 9 ] || return 1 local n=$(( 10#${d:-0} )) [ "$n" -ge "$2" ] || return 1 printf '%s\n' "$n" } # mam_int_knob # # The value of $ENV_NAME through mam_int; unset or empty gives , an # invalid value gives with a warning on stderr. mam_int_knob() { local raw="${!1-}" [ -n "$raw" ] || { printf '%s\n' "$2"; return 0; } mam_int "$raw" "$3" && return 0 echo "WARNING: $1=$raw ignored (not a decimal integer >= $3); using $2" >&2 printf '%s\n' "$2" }