bug-bash
[Top][All Lists]
Advanced

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

Re: BASH_XTRACEFD=1 read variable stdout flushing?


From: Dave Drambus
Subject: Re: BASH_XTRACEFD=1 read variable stdout flushing?
Date: Mon, 16 Jan 2023 18:43:02 -0600

Thanks for the explanation and the help!

On Mon, Jan 16, 2023 at 1:53 PM Chet Ramey <chet.ramey@case.edu> wrote:

> On 1/16/23 12:36 PM, dave.drambus@gmail.com wrote:
> > Configuration Information [Automatically generated, do not change]:
> > Machine: x86_64
> > OS: linux-gnu
> > Compiler: gcc
> > Compilation CFLAGS: -g -O2 -flto=auto -ffat-lto-objects -flto=auto
> -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security
> -Wall
> > uname output: Linux sulfur 5.15.0-58-generic #64-Ubuntu SMP Thu Jan 5
> 11:43:13 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
> > Machine Type: x86_64-pc-linux-gnu
> >
> > Bash Version: 5.1
> > Patch Level: 16
> > Release Status: release
> >
> > Description:
> >
> >       I have `set -x` and `BASH_XTRACEFD=1` set in a simple script that
> I have in
> >       a cron job. I'd like to see the progress of the script when run
> manually,
> >       hence `set -x`, but I want the output to go to stdout rather than
> stderr so
> >       that cron emails me only when there is an actual failure. With this
> >       configuration, the `read` command is erroneously reading the "+"
> prompt
> >       output from `set -x` into the variable. It seems like stdout is
> not getting
> >       flushed propertly.
> >
> > Repeat-By:
> >       The following script attempts to read the first column of data,
> line by
> >       line, into a variable called `$var`. With `BASH_XTRACEFD` unset, it
> >       propertly prints r1c1, r2c1, r3c1, r4c1. With `BASH_XTRACEFD=1`,
> `$var`
> >       gets set to +, ++, r2c1, r3c1, r4c1. Maybe I am just being dumb?
> >
> > ```
> > #!/usr/bin/env bash
> >
> > BASH_XTRACEFD=1
> > set -x
> >
> > data=$(cat << END
> > r1c1    r1c2    r1c3\n
> > r2c1    r2c2    r2c3\n
> > r3c1    r3c2    r3c3\n
> > r4c1    r4c2    r4c3
> > END
> > )
> >
> > echo -e $data | while read -r var junk; do
> >      echo "var = $var"
> > done
> > ```
>
> As you discovered, BASH_XTRACEFD makes the xtrace output write to the
> designated file descriptor. If you write to fd 1, you're going to write the
> trace output into the pipe, where `read' will see it.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>
>


reply via email to

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