Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Sometimes I'll attempt a (2>&1) redirection and some/all of the resulting output appears to be silenced.

e.g.

wget -O- http://localhost/test.txt 2>&1

I would expect to see a merge of contents of test.txt and the output of the transfer, but instead results in only the output to stderr and not the output to stdout:

--2013-03-18 14:53:41--  http://localhost/test.txt
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9 [text/plain]
Saving to: `STDOUT'

 0% [                                       ] 0           --.-K/s              1100%     
    [======================================>] 9           --.-K/s   in 0s      

2013-03-18 14:53:41 (2.09 MB/s) - written to stdout [9/9]

Shouldn't stdout have written it to the screen?

And yet:

wget -O- http://localhost/test.txt 2>&1 > test.stdout

results in the file being written to test.stdout as expected.

Similarly I have seen this behaviour with expect scripts (send_user) and multiple grep pipes. e.g.

/myexpectscript | grep 'blah'

works and filters all lines but those containing 'blah', but

/myexpectscript | grep 'foo' | grep 'bar'

results in blank output.

How I discovered this was when I was wanting to use tee to duplicate output. e.g.

wget -O- http://localhost/test.txt 2>&1 | tee

results in no output at all, whereas:

wget -O- http://localhost/test.txt | tee

results in:

--2013-03-18 15:16:42--  http://localhost/ddns/checkip.php
Resolving localhost... 127.0.0.1
Connecting to localhost|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9 [text/plain]
Saving to: `STDOUT'

127.0.0.1100%[======================================>] 9           --.-K/s   in 0s      

2013-03-18 15:16:42 (2.30 MB/s) - written to stdout [9/9]

(Note line 8 lists the contents of test.txt "127.0.0.1")

Under what circumstances is redirected output blocked? Why does a supposed output to stdout by wget only work when redirected to a file or command?

share|improve this question
    
Please check the state of sdtout and stderr of the calling shell: ls -l /proc/$$/fd –  Hauke Laging Mar 18 '13 at 6:11
add comment

1 Answer

Here wget -O- http://localhost/test.txt 2>&1 does write the downloaded content to stdout.

grep 'foo' | grep 'bar' obviously results in no output if there are no lines containing both "foo" and "bar".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.