bug-bash
[Top][All Lists]
Advanced

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

BASH_XTRACEFD=1 read variable stdout flushing?


From: dave . drambus
Subject: BASH_XTRACEFD=1 read variable stdout flushing?
Date: Mon, 16 Jan 2023 11:36:18 -0600 (CST)

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
```


Fix:
        [Description of how to fix the problem.  If you don't know a
        fix for the problem, don't include this section.]



reply via email to

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