On Thu, Jun 6, 2013 at 8:11 AM, Eric Blake <eblake@xxxxxxxxxx> wrote: > ----- Original Message ----- > >> >> A more robust, (and more portable), formulation may be: >> >> echo $var | grep '^+\{0,1\}[0-9]\{1,\}$' > /dev/null 2>&1 > > Why fork, when straight shell will do? yea, forking for grep is probably going to make your script significantly slower > > case $var in > +*) tmp=$var ;; > *) tmp=+$var ;; > esac > case $tmp in > +*[!0-9]* | +) echo "not numeric" ;; > *) echo integer ;; > esac i've found through benchmarks that certain shells (dash and bash and ksh) are optimized towards if statements over case ones i would parse the number like this for best performance, but make a copy for $var because it's destructive: trim() { n=$1 v=$2 while [ ${#v} -ne $n ] do v=${v%?} done echo $v } if [ -z "$var" ]; then echo "ENOTNUM $var" >&2 exit 2 fi if [ $(trim 1 $var) = '+' ]; then echo "we got a signed number; begin normalizing procedure" var=${var#?} fi while [ ${#var} -ne 0 ] do if [ ${var%${var#?}} = '0' ] || [ ${var%${var#?}} = '1' ] || [ ${var%${var#?}} = '2' ] || [ ${var%${var#?}} = '3' ]; then echo "digit is base 4" else echo "ENOTNUM $var" >&2 exit 2 fi var=${var#?} done > > Again, when placing this in autoconf.ac, you need to quote the > [0-9] since m4 eats one level of [], either by writing [[0-9]] > in place, or by using [] around the entire case snippet. > > -- > Eric Blake eblake@xxxxxxxxxx +1-919-301-3266 > Libvirt virtualization library http://libvirt.org > > _______________________________________________ > Autoconf mailing list > Autoconf@xxxxxxx > https://lists.gnu.org/mailman/listinfo/autoconf _______________________________________________ Autoconf mailing list Autoconf@xxxxxxx https://lists.gnu.org/mailman/listinfo/autoconf