0

I'm trying to capture output and seeing strange behavior. Some output will only show if no redirection is used. Can be replicated reliably with this docker example:

FROM alpine
RUN apk add git
RUN \
  echo "echo \"----NO REDIRECT:\" && no_redirection=\$(git clone http://github.com/srfrnk/jabos-examples.git /tmp1)" >> test &&\
  echo "echo \"----NO REDIRECT CAPTURED: \${no_redirection}\"" >> test &&\
  echo "echo \"----REDIRECT:\" && redirection=\$(git clone http://github.com/srfrnk/jabos-examples.git /tmp2 2>&1)" >> test &&\
  echo "echo \"----REDIRECT CAPTURED: \${redirection}\"" >> test &&\
  cat test &&\
  chmod +x test
ENTRYPOINT [ "/bin/sh","-c"]
CMD ["./test"]

This is essentially running the following script in a container:

echo "----NO REDIRECT:" && no_redirection=$(git clone http://github.com/srfrnk/jabos-examples.git /tmp1)
echo "----NO REDIRECT CAPTURED: ${no_redirection}"
echo "----REDIRECT:" && redirection=$(git clone http://github.com/srfrnk/jabos-examples.git /tmp2 2>&1)
echo "----REDIRECT CAPTURED: ${redirection}"

So there are two cases running git - one with redirection and one without. Both output are captured into a variable which is then printed.

If you build and run this (docker build . -t test && docker run -it test) you can see the following output:

----NO REDIRECT:
Cloning into '/tmp1'...
warning: redirecting to https://github.com/srfrnk/jabos-examples.git/
remote: Enumerating objects: 195, done.
remote: Counting objects: 100% (195/195), done.
remote: Compressing objects: 100% (137/137), done.
remote: Total 195 (delta 106), reused 122 (delta 44), pack-reused 0
Receiving objects: 100% (195/195), 40.92 KiB | 498.00 KiB/s, done.
Resolving deltas: 100% (106/106), done.
----NO REDIRECT CAPTURED: 
----REDIRECT:
----REDIRECT CAPTURED: Cloning into '/tmp2'...
warning: redirecting to https://github.com/srfrnk/jabos-examples.git/

P.S. This isn't a docker thing - happens in bash on my Ubuntu box as well. I used docker to have an environment agnostic example.

So my question is where does the output after warning: ... go to and how can I redirect/capture that?

1 Answer 1

0

The text you're hoping to capture is going to STDERR (file stream 2).

In case 1, you're letting STDERR go to the terminal by default.
In case 2, you're not running git.
In case 3, you're capturing STDERR along with STDOUT in the variable no_redirection.
In case 4, you're not running git.

Your STDERR text is not being captured in your "capture" cases because your "capture" cases aren't actually running git because of how you're using quotes in your nested echo statements on the ----CAPTURED lines. The double quotes are escaping the single quotes, so the single quotes are not escaping the variable expansion, so the variable is expanded at the time the "test" script file is being created, not when it's being executed. So the no_redirection and redirection variables don't exist when they get expanded at test-script-creation-time, so they are simply removed and replaced with nothing. So if you look inside your "test" script file, I think you'll discover it doesn't contain what you thought it would contain.

The STDERR text is being captured in your non-capture "redirect" case; it's all being assigned as the value of the shell variable redirection. That's because you redirected STDERR into STDOUT with 2>&1, and you captured STDOUT (and thus STDERR) by using redirection=$(…) to assign STDOUT to the variable redirection.

2
  • I'm not sure where you see 4 cases. The code is running two cases one with redirection and one without. In both you can see all the output being sent to any streams in the TTY as well as what is being captured. However the output is not the same. Hence the question. If this output would have gone into STDERR - why doesn't it get printed to the TTY?
    – srfrnk
    Jan 24, 2022 at 7:31
  • I edited the example to make it clearer - as you can see the capture clearly doesn't have the entire output in the second case as in the first.
    – srfrnk
    Jan 24, 2022 at 8:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .