bug-bash
[Top][All Lists]
Advanced

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

Re: $() does not handle nesting with case - parser precedence?


From: Dennis Williamson
Subject: Re: $() does not handle nesting with case - parser precedence?
Date: Sun, 28 Jun 2015 14:57:59 -0500



On Sat, Jun 27, 2015 at 2:48 PM, Nathan Neulinger <address@hidden> wrote:
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-redhat-linux-gnu' -DCONF_VENDOR='redhat' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib  -D_GNU_SOURCE -DRECYCLES_PIDS -DDEFAULT_PATH_VALUE='/usr/local/bin:/usr/bin'  -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches  -m64 -mtune=generic
uname output: Linux skyhawk.home.neulinger.org 3.19.3-200.fc21.x86_64 #1 SMP Thu Mar 26 21:39:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

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

Description:

        If $() includes a case statement nested within it, the parser is not matching ) as closing the case,
        but rather the $(. This behavior is different between bash and other shells. ksh/busybox
        both process this without error, but I do not know which is officially "correct".

        Test Case:
---------------
testing=$(
    echo test | while read line; do
        case $line in
             test)  echo saw test ;;
             *)     echo other ;;
        esac
    done
)

echo result: $testing
--------------

        Expected output:

result: saw test

        Actual output:

parse-bug.sh: line 6: syntax error near unexpected token `;;'
parse-bug.sh: line 6: `         test)  echo saw test ;;'


        Workaround:  Use (test) instead of test) in the nested code


Repeat-By:
        Run script with that syntax.



--
------------------------------------------------------------
Nathan Neulinger                       address@hidden
Neulinger Consulting                   (573) 612-1412



You can use the full syntax of case by surrounding the cases with both opening and closing parentheses:

testing=$(
    echo test | while read line; do
        case $line in
             (test)  echo saw test ;;
             (*)     echo other ;;
        esac
    done
)

POSIX shows the opening parentheses as optional, but does not describe their use or when they might be necessary.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04

--
Visit serverfault.com to get your system administration questions answered.

reply via email to

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