bug-bash
[Top][All Lists]
Advanced

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

Intriguing error with arithmetic evaluation


From: NO REPLY
Subject: Intriguing error with arithmetic evaluation
Date: Fri, 12 Aug 2016 14:22:26 +0530

Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I../. -I.././include -I.././lib  -D_FORTIFY_SOURCE=2 -g 
-O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat 
-Werror=format-security -Wall
uname output: Linux hdara1-wsl 3.13.0-85-generic #129-Ubuntu SMP Thu Mar 17 
20:50:15 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.3
Patch Level: 11
Release Status: release

Description:
        I have a few increment expressions used as ((level++)) and only one of 
those is giving an error. When used with set -e, bash aborts
        execution. Using ERR trap, I was able to identify the expression.


Repeat-By:

        I have this simple logic in which I am running a sequence of commands 
conditionally, based on what a user provided level is.
        The intention is to only run those groups of commands appropriate for 
the level specified. To demonstrate the idea, here is
        a simple script:

        >>> CUT HERE <<<
        #!/usr/bin/env bash
        set -eEu -o pipefail
       
        trap 'echo "*** ERROR: Detected error on command: $BASH_COMMAND at 
line: $LINENO"' ERR
       
        DEBUG_LEVEL=$1
       
        function debug_mode() {
            declare -i level=0
            (( level >= DEBUG_LEVEL )) || {
                echo "Running: level $((level+1)) commands"
                ((level++))
            }
            (( level >= DEBUG_LEVEL )) || {
                echo "Running: level $((level+1)) commands"
                ((level++))
            }
        }
       
        debug_mode
        >>> CUT HERE <<<

        When this script works (as is the case on "GNU bash, version 
3.2.57(1)-release (x86_64-apple-darwin15)"), you would see the below 
interaction:

        $ debug.sh 0
        $ debug.sh 1
        Running level 1 commands
        $ debug.sh 2
        Running level 1 commands
        Running level 2 commands

        However, when run on this version of bash (or even on GNU bash, version 
4.1.2(1)-release (x86_64-redhat-linux-gnu)), the first "((level++))" results in 
an error:

        $ debug.sh 0
        $ debug.sh 1
        Running: level 1 commands
        *** ERROR: Detected error on command: ((level++)) at line: 8

        If I replace the first "((level++))" with ": $((level++))" or 
"level+=1", then it goes through fine, and the second "((level++))" doesn't 
generate any error.




reply via email to

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