1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #!/bin/bash
-
- ffoo import core
-
-
- is_integer() {
- #
- # True if $1 is integer value
- #
- # This is achieved by checking if
- #
- local val="$1"
- test 0 -ge "$val" 2>/dev/null || test 0 -le "$val" 2>/dev/null
- case $? in
- 0) return 0 ;;
- 2) return 1 ;;
- *) warn "weird exit status in is_integer()"; return 2 ;;
- esac
- }
-
-
- bool() {
- #
- # True if $1 is true
- #
- # bool [--like-sh|--like-c] value
- #
- # Returns with zero exit status for $1 values of "true",
- # "yes" or "y", 1 for for "false", "no" or "n" (only common
- # English is supported). For any other non-integer value,
- # prints warning and exits with status of 2.
- #
- # For integer values, if --like-sh is specified (default),
- # zero produces zero, but any other value produces 1. Use
- # --like-c to turn this logic around, i.e. as C works:
- # anything else than zero is considered true, resulting in
- # zero exit status.
- #
- local mode=sh
- while true; do case "$1" in
- --like-sh) mode=sh; shift ;;
- --like-c) mode=c; shift ;;
- *) break ;;
- esac done
- local value="$1"
- local reset_shopt="$(shopt -p)"
- debug -v value
- if is_integer "$value";
- then
- case "$mode:$value" in
- c:0) return 1 ;;
- c:*) return 0 ;;
- sh:0) return 0 ;;
- sh:[0-9]*) return 1 ;; # positive bash exit status
- *) warn "unexpected 'mode:value': $mode:$value"; return 2 ;;
- esac
- else
- case "${value,,}" in
- true|yes|y) return 0 ;;
- false|no|n) return 1 ;;
- "") usage_is -E "[-n] value"; return 2 ;;
- *) warn "cannot determine true-ness: $value"; return 2 ;;
- esac
- fi
- }
|