Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #14125

Conditions with logical operators and nested groups execute "if" and "else"

From Uriel <url.15.104@gmail.com>
Newsgroups gnu.bash.bug
Subject Conditions with logical operators and nested groups execute "if" and "else"
Date 2018-05-21 16:17 -0500
Message-ID <mailman.206.1526969874.1292.bug-bash@gnu.org> (permalink)

Show all headers | View raw


Configuration Information:
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  -Wdate-time
-D_FORTIFY_SOURCE=2 -g -O2
-fdebug-prefix-map=/build/bash-DWMIDv/bash-4.4.18=.
-fstack-protector-strong -Wformat -Werror=format-security -Wall
-Wno-parentheses -Wno-format-security
uname output: Linux HPgS 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1
(2018-05-07) x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 19
Release Status: release

Description:
As you know, a conditional is of the type:
if [[ EXPRESSION ]]; then TRUE CONDITION; else ALTERNATIVE RESULT; fi

Or with logical operators and groups:

[[ EXPRESSION ]]; && { TRUE CONDITION; } || { ALTERNATIVE RESULT; }

Within each of the conditionals there may be more nested, written the first
or
second way they should give the same result, however, using the logical
operators form to obfuscate the code a little, I find that my conditional
(which has another one in it) is executed in the first condition and also
in the
second one. Analyzing for a long time if it was a mistake in rewriting I saw
that it worked the same way, the problem seems to be one of the operator
«!» in
a «=~» conditional because when testing this same notation with other
conditions
it does not seem to have an error.

Repeat-By:
#!/bin/bash
# In the second example passing through «[[ ! "$OPT" =~ ^- ]] && {
OPT="-$OPT"; }» should end, however, it continues with the following

OPT="-e-e-e--debug"; OPT_PERFECT="-e"

echo "INICIAL $OPT"

if [[ "$OPT" =~ ^- ]]; then
    OPT="${OPT#$OPT_PERFECT}"
    echo "REDUCCION TIPO 1 $OPT"
    if [[ ! "$OPT" =~ ^- ]]; then
        OPT="-$OPT"
        echo "CONDICIONAL $OPT"
    fi
else
   OPT="${OPT#$OPT_PERFECT}"
   echo "REDUCCION TIPO 2 $OPT"
fi

echo "----------------------------------------------"

OPT="-e-e-e--debug"; OPT_PERFECT="-e"

echo "INICIAL $OPT"

[[ "$OPT" =~ ^- ]] && {
    OPT="${OPT#$OPT_PERFECT}"
    echo "REDUCCION TIPO 1 $OPT"
    [[ ! "$OPT" =~ ^- ]] && {
        OPT="-$OPT"
    }
} || {
   OPT="${OPT#$OPT_PERFECT}"
   echo "REDUCCION TIPO 2 $OPT"
}

Fix:
No big deal, you can use operator alternatives instead of
«[[ ! "$OPT" =~ ^- ]] && { OPT="-$OPT"; }» you can use
«[[ "$OPT" =~ ^- ]] || { OPT="-$OPT"; }».

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Conditions with logical operators and nested groups execute "if" and "else" Uriel <url.15.104@gmail.com> - 2018-05-21 16:17 -0500

csiph-web