autoconf
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: process result code in if


From: Andres Perera
Subject: Re: process result code in if
Date: Thu, 6 Jun 2013 18:26:55 -0430

On Thu, Jun 6, 2013 at 8:11 AM, Eric Blake <address@hidden> 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   address@hidden    +1-919-301-3266
> Libvirt virtualization library http://libvirt.org
>
> _______________________________________________
> Autoconf mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/autoconf



reply via email to

[Prev in Thread] Current Thread [Next in Thread]