bug-bash
[Top][All Lists]
Advanced

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

Re: Bug in return code from "bash -c '! command'"


From: Chet Ramey
Subject: Re: Bug in return code from "bash -c '! command'"
Date: Fri, 11 Mar 2005 16:47:47 -0500
User-agent: Mozilla Thunderbird 1.0 (Macintosh/20041206)

address@hidden wrote:
Environment:  Bash 3.00.14(1) running on i386 under Fedora Core 3 Linux.

In Bash 3.00, it seems that the return code from bash is not
duplicated from a command specified by -c if the command starts with
"!".

To reproduce this, write a script:

        #! /bin/bash

        set -x

        bash --version

        # Create a file baz containing one line containing @.
        cat >baz <<EOF
        @
        EOF

        grep @ baz ; echo $?

        ! grep @ baz ; echo $?

        bash -c "grep @ baz" ; echo $?

        bash -c "! grep @ baz" ; echo $?

        exit

The output it generates is:

        + bash --version
        GNU bash, version 3.00.14(1)-release (i386-redhat-linux-gnu)
        Copyright (C) 2004 Free Software Foundation, Inc.
        + cat
        + grep @ baz
        @
        + echo 0
        0
        + grep @ baz
        @
        + echo 1
        1
        + bash -c 'grep @ baz'
        @
        + echo 0
        0
        + bash -c '! grep @ baz'
        @
        + echo 0
        0
        + exit

In the final test, 'bash -c "! grep @ baz"', the command '! grep @
baz' returns 1, but bash returns 0.

Thanks, fixed.

I've attached the patch.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet )
                                                Live...Laugh...Love
Chet Ramey, ITS, CWRU    address@hidden    http://cnswww.cns.cwru.edu/~chet/
*** ../bash-3.0-patched/builtins/evalstring.c   Tue Jun  3 13:53:12 2003
--- builtins/evalstring.c       Fri Mar 11 16:44:56 2005
***************
*** 234,238 ****
               *   we have parsed the full command (string == '\0') AND
               *   we have a simple command without redirections AND
!              *   the command is not being timed
               * THEN
               *   tell the execution code that we don't need to fork
--- 234,239 ----
               *   we have parsed the full command (string == '\0') AND
               *   we have a simple command without redirections AND
!              *   the command is not being timed AND
!              *   the command's return status is not being inverted
               * THEN
               *   tell the execution code that we don't need to fork
***************
*** 242,246 ****
                  command->type == cm_simple &&
                  !command->redirects && !command->value.Simple->redirects &&
!                 ((command->flags & CMD_TIME_PIPELINE) == 0))
                {
                  command->flags |= CMD_NO_FORK;
--- 243,248 ----
                  command->type == cm_simple &&
                  !command->redirects && !command->value.Simple->redirects &&
!                 ((command->flags & CMD_TIME_PIPELINE) == 0) &&
!                 ((command->flags & CMD_INVERT_RETURN) == 0))
                {
                  command->flags |= CMD_NO_FORK;

reply via email to

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