#!/bin/bash # Test 4 function is_num, is_unum, is_int and is_uint under different shell implementations. # (C) 2020-2022 Felix Hauri - felix@f-hauri.ch # Licensed under terms of GPL v3. www.gnu.org AllTests=(1 42 -3 +42 +3. .9 3.14 +3.141 -31.4 '' . 3-3 3.1.4 3a a3 blah 'Good day!') AllResult=TTTTTTTTFTFTFTFTFFFTFFTTFFTTFFFTFFFTFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF explain=false; case $1 in -h | --help ) explain=true; shift ;; esac AllShells=(bash dash 'busybox sh' ksh zsh "$@") explain() { local allShellStr shopt -s extglob echo "Explanation: This test script will test all this 4 functions:" sed '/()/s|^| |p;d' < <(testScript) printf 'under %d differents shells:\n' ${#AllShells[@]} allShellStr=("${AllShells[@]:0:${#AllShells[@]}-2}") printf -v allShellStr[0] '%s, ' "${allShellStr[@]@Q}" printf ' %s and %s.\n (You could add more shells as arguments on command line.)\n' \ "${allShellStr[0]%, }" "${AllShells[-1]@Q}" printf "Against %d different strings (for 4 functions), there are %d results:\n" \ ${#AllTests[@]} $((4*${#AllTests[@]})) eval "printf ' %-12q %10s %10s %10s %10s\n' String Is\ UInt Is\ Int Is\ UNum Is\ Num "$( declare -A "fmtd=( [F]=false [T]=true )" for ((i=0;i<${#AllResult};i+=4)); do echo "${AllTests[i/4]@Q}" ${fmtd[${AllResult:i:1}]^} ${fmtd[${AllResult:i+1:1}]^}\ ${fmtd[${AllResult:i+2:1}]^} ${fmtd[${AllResult:i+3:1}]^} done ) echo Run tests: } testScript() { cat <<-eoscript is_uint() { case \$1 in '' | *[!0-9]* ) return 1;; esac ;} is_int() { case \${1#[-+]} in '' | *[!0-9]* ) return 1;; esac ;} is_unum() { case \$1 in '' | . | *[!0-9.]* | *.*.* ) return 1;; esac ;} is_num() { case \${1#[-+]} in '' | . | *[!0-9.]* | *.*.* ) return 1;; esac ;} outstr=''; for var in ${AllTests[@]@Q}; do for func in is_uint is_int is_unum is_num ;do if \$func "\$var"; then outstr="\$outstr"T else outstr="\$outstr"F fi done ;done case \$outstr in $AllResult ) echo Success ;; * ) echo Failed ;; esac eoscript } $explain && explain for shell in "${AllShells[@]}"; do if $explain; then printf " %-14s " "${shell@Q}" else printf "%-12s " "${shell}" fi $shell < <(testScript) 2>&1 | xargs done