Brendan Hoar:
> On Sat, Feb 12, 2022 at 7:03 AM David Hobach <[email protected]> wrote:
> 
>> Dear all,
>>
>> just stumbled across it and was wondering what a reviewer would
>> expect from this code to do:
>>
>> ```
>> #!/bin/bash
>>
>> function badCode {
>> echo "bad code executed"
>> }
>>
>> function testCode {
>> #pick some existing file, nonexisting works too though
>> echo "/etc/passwd"
>> }
>>
>> function tfunc {
>>    local foo=
>>    foo="$(testCode)" || {echo "foo";}
>>    cat "$foo" || {
>>          badCode
>>          case $? in
>>           *)
>>             exit 1
>>          esac
>> }
>> }
>>
>> echo "Finished."
>> ```
>>
>> At least on my amchine it executes "badCode" in both domU and dom0. I
>> guess it's a bash bug and reported it accordingly, but anyway...
> 
> 
> I’m not exactly sure what’s going on (and can’t bash right now) but
> output grouping requires white space around { and } so that’s at least
> one problem on the foo= assignment line. Maybe it’s being interpreted
> as closing the function definition early?

Yes you are on the right track here, I think. One problem is the missing
space after the opening {

    $ false || {echo "foo"; 
    bash: {echo: command not found
    $ false || {echo "foo";}
    bash: syntax error near unexpected token `}'
    $ 

So the } after the "foo"; closes the function. But why don't we get an
error for the later } that don't match? Because bash operates line by
line (remember those self extracting archives, that are shell scripts
followed by a tar). If you change the *) into, say, x) (or anything that
doesn't match $?) you see it:

    $ ./t2
    cat: '': No such file or directory
    bad code executed
    ./t2: line 22: syntax error near unexpected token `}'
    ./t2: line 22: `}'
    $ 

So this is probably not even a bug. Thanks for the nice example David
(apropos shell: set -e semantics are also "fun").

Simon

-- 
You received this message because you are subscribed to the Google Groups 
"qubes-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/qubes-devel/5a120080-f1a2-8348-8358-fb0d5504b22b%40ipsumj.de.

Reply via email to