Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > gnu.bash.bug > #15547
| Path | csiph.com!goblin2!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail |
|---|---|
| From | achurch@achurch.org (Andrew Church) |
| Newsgroups | gnu.bash.bug |
| Subject | Re: Writing to /dev/stderr overwrites xtrace data |
| Date | Thu, 31 Oct 2019 12:48:00 +0900 |
| Lines | 38 |
| Approved | bug-bash@gnu.org |
| Message-ID | <mailman.62.1572493736.13325.bug-bash@gnu.org> (permalink) |
| References | <20191031011023.B4AC1E0051@birch.ghs.com> <5dba5997.12501@msgid.achurch.org> |
| NNTP-Posting-Host | lists.gnu.org |
| Content-Type | text/plain; charset=utf-8 |
| X-Trace | usenet.stanford.edu 1572493736 5231 209.51.188.17 (31 Oct 2019 03:48:56 GMT) |
| X-Complaints-To | action@cs.stanford.edu |
| Cc | bug-bash@gnu.org |
| To | jono.chadwell@gmail.com |
| Envelope-to | bug-bash@gnu.org |
| X-wmail-status | normal |
| In-Reply-To | <20191031011023.B4AC1E0051@birch.ghs.com> |
| X-Mailer | MMail v5.51 |
| X-detected-operating-system | by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] |
| X-Received-From | 60.39.233.218 |
| X-BeenThere | bug-bash@gnu.org |
| X-Mailman-Version | 2.1.23 |
| Precedence | list |
| List-Id | Bug reports for the GNU Bourne Again SHell <bug-bash.gnu.org> |
| List-Unsubscribe | <https://lists.gnu.org/mailman/options/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=unsubscribe> |
| List-Archive | <https://lists.gnu.org/archive/html/bug-bash> |
| List-Post | <mailto:bug-bash@gnu.org> |
| List-Help | <mailto:bug-bash-request@gnu.org?subject=help> |
| List-Subscribe | <https://lists.gnu.org/mailman/listinfo/bug-bash>, <mailto:bug-bash-request@gnu.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <5dba5997.12501@msgid.achurch.org> |
| Xref | csiph.com gnu.bash.bug:15547 |
Show key headers only | View raw
> I enabled xtrace to try and debug a bash script running on a validation
> server. The script stored its stdout and stderr to a log file. I expected
> to see trace data in the log file, but instead found a single log statement,
> a bunch of NULL bytes, and the line '+ exit 1'.
This is expected behavior given the your example script.
>echo "#!/usr/bin/env bash" > script.sh
>echo "set -o xtrace" >> script.sh
>echo "echo to_stderr > /dev/stderr" >> script.sh
This command causes the shell to open a new file descriptor for
/dev/stderr with the O_TRUNCATE flag, truncating the file to empty.
>chmod 755 script.sh
>./script.sh 2> stderr_output
Since the script is run with stderr redirected to a file, /dev/stderr
is a symbolic link to that file:
$ (stat -c%N /dev/stderr /proc/self/fd/2) 2>/tmp/stderr_output
'/dev/stderr' -> '/proc/self/fd/2'
'/proc/self/fd/2' -> '/tmp/stderr_output'
So the echo command in the generated script truncates the file
"stderr_output" before writing "to_stderr\n", and you lose any xtrace
log lines which were previously written. Bash still has an open file
descriptor to stderr, which has its own seek position, so if you add
another command (like "exit 1") to the generated script after the echo,
the xtrace line generated by that command will be written at that
position in the file, with the intervening space filled by null bytes.
The solution is to use ">&2" rather than explicitly referencing
/dev/stderr.
--Andrew Church
http://achurch.org/
Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread
Re: Writing to /dev/stderr overwrites xtrace data achurch@achurch.org (Andrew Church) - 2019-10-31 12:48 +0900
csiph-web