You are not logged in.

#1 2012-01-25 23:27:23

inkdinky
Member
Registered: 2011-12-08
Posts: 66

[SOLVEDish] bash: wget doesn't get full webpage when piped

Method 1

wget ${1} -O ${Webpage} 2>&1 | grep -q "200 OK"

Method 2

wget ${1} -O ${Webpage} & 2>&1 | grep -q "200 OK"

Method 1 works on an PIII @750MHz running Ubuntu 10.10
Method  1 doesn't work on a PIV @2.4 GHz or a Celeron @1GHz running Arch

By "doesn't work" I get about 3/4 of a page of text of the html webpage I'm going after.
Have I somehow screwed up the way the pipes work or are my assumptions about pipes wrong? Or is it the processor speeds?
As far as the pipes go I thought the output of the first command was piped to the second command, yet here it seems that somehow the piping is cutting the previous command short????

Thanks in advance.

Last edited by inkdinky (2012-01-26 19:00:41)

Offline

#2 2012-01-26 00:01:22

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

What about Method 2? Does it work?

Offline

#3 2012-01-26 00:23:10

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Yeah. Method 2 works.
This just makes me wonder if there is an over-arching problem with timing and or things happening too fast when programming in bash. I say this bc as I rework the script I'm having problems with the ftp using here documents and commands. Not sure what the problem is there just yet but it isn't a problem on the Ubuntu box yet it is on the Arch Celeron.

Offline

#4 2012-01-26 00:31:39

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Are you using the bash on Ubuntu? IIRC the default shell is dash.

Offline

#5 2012-01-26 00:43:52

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Well I am calling it with bash
bash script.sh
and the shebang is followed by /bin/bash in the script. I'm calling the script via
bash script.sh
on all machines.

Offline

#6 2012-01-26 00:45:49

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

echo $SHELL
returned
/bin/bash
on all machines.

Offline

#7 2012-01-26 00:47:01

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

I don't think Method 2 is doing what you expect.  The '&' end should the wget command, invalidating the redirection; effectively like running:

$ wget ${1} -O ${Webpage}
$ 2>&1 | grep -q "200 OK"

I'm in Windows now, but '2>&1' is a valid command in cygwin.  Surprising to me anyway.

I think the correct syntax should be

wget ${1} -O ${Webpage} 2>&1 & | grep -q "200 OK"

Unless I'm completely missing the point.

Last edited by alphaniner (2012-01-26 00:49:42)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#8 2012-01-26 01:53:44

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

@alphaniner Ok I'll buy that it isn't doing what I'm expecting, but your proposed solution doesn't work.

wget [url]http://www.yahoo.com[/url] -O /tmp/Yahoo 2>&1  & | grep -q "200 OK"; less /tmp/Yahoo
bash: syntax error near unexpected token `|'

So.....
The point still remains that Method 1 fails on the higher speed machines for some reason.

wget [url]http://www.yahoo.com[/url] -O /tmp/Yahoo 2>&1 | grep -q "200 OK"; less /tmp/Yahoo

results in a partial webpage

wget [url]http://www.yahoo.com[/url] -O /tmp/Yahoo & 2>&1  | grep -q "200 OK"; less /tmp/Yahoo

results in the full webpage.

Offline

#9 2012-01-26 02:04:58

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Yeah, my mistake.  When you put the & on the end you're effectively doing

$ wget ...
$ | grep ...

which is obviously not going to work.

But I stand by the point that in method 2 you're piping the output of 2>&1 (which is precisely nothing) rather than any output of wget, so the fact that succeeds is pretty much irrelevant.

Unfortunately my cygwin install doesn't include wget, so I can't do any testing with the command.

Maybe if you explain exactly what you're trying to accomplish.  It seems like you want some kind of notification of wget's successful completion, and there are a lot of ways to accomplish that.

Last edited by alphaniner (2012-01-26 02:24:39)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#10 2012-01-26 02:07:24

fschiff
Member
Registered: 2011-10-06
Posts: 71

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

I'd have to look it up but doesn't 2>&1
redirect stderr to stdout?

Is that what you want?

Offline

#11 2012-01-26 02:21:27

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Nevermind.

Last edited by alphaniner (2012-01-26 02:22:28)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#12 2012-01-26 02:32:59

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

The -q option to grep causes it to exit immediately when a match is found (see grep(1)). When the reading process of a pipe exits, the writing process receives SIGPIPE, which in most cases terminates it, though it can be caught. Hence, the moment grep reads "200 OK", wget is signaled. There may or may not be enough time for it to finish writing to ${Webpage}.

Method 2 does not do what you want at all. It's equivalent to writing this:

$ wget ${1} -O ${Webpage} &
$ 2>&1 | grep -q "200 OK"

The first line runs wget in the background. The second line pipes the output of the command 2>&1, which outputs nothing, to grep.

Edit for further information:

If you want to test for successful retrieval of ${1}, you can check the exit status of wget. If you want to check specifically for the string "200 OK", you can use grep in a way that does not cause it to prematurely close the pipe. Also, note that the curly braces around your variable names are superfluous.

Last edited by Peasantoid (2012-01-26 02:43:29)

Offline

#13 2012-01-26 02:53:51

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

I appreciate the help guys/gals.
I guess I really just want to make sure that wget exited successfully and wasn't redirected. So I do want the "200 ok" and not some other exit code.
From Peasantoid's explanation I think that the old PIII is slow enough that wget finishes before grep is done.
So is there a preferred method to capturing wget's exit status? man wget didn't seem to be much help.

Offline

#14 2012-01-26 03:05:25

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

inkdinky wrote:

I guess I really just want to make sure that wget exited successfully and wasn't redirected. So I do want the "200 ok" and not some other exit code.

Do you mean a redirect as in HTTP 3xx or receiving the Location header from the server? In that case, wget will print "200 OK" no matter what happens, so long as it eventually ends up at a URI that yields HTTP 200. If you grep for "200 OK", you will also need to check that only one request occurred. You might try grep -v "$something" against the output, where $something is a string that only occurs in the event of a redirect.

Offline

#15 2012-01-26 03:09:12

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

I'm not really expecting a redirect but is there a better way to get the status code from wget? As it stands my way isn't foolproof.

Offline

#16 2012-01-26 03:29:04

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Just to clarify, do you mean "status code" as in the exit status of wget or the HTTP status? If you want the HTTP status, you need to retrieve the wget output in a way that avoids the SIGPIPE problem. For example, you could read the output into a string and grep against that, or you could use the pipe and suppress grep's output without using -q. If it's the exit status, you can use $?, &&, ||, if, and so on.

Offline

#17 2012-01-26 04:00:04

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Maybe you could use process substitution instead of a pipe:

grep "200 OK" < <(wget ... 2>&1)

As I understand the concept, it's sort of like redirecting the output of wget to a file then grepping the file, without actually making a file*.  It may have the same limitation that Peasantoid mentioned, but I don't think so.

* The first < is actually input redirection.  But when grep isn't told to search a file, it searches stdin.  And input redirection sends the contents of a file to stdin.  So

grep "yadda" file

and

grep "yadda" < file

do pretty much the same thing I think.  The result is the same anyway.

Last edited by alphaniner (2012-01-26 04:55:52)


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#18 2012-01-26 18:53:57

inkdinky
Member
Registered: 2011-12-08
Posts: 66

Re: [SOLVEDish] bash: wget doesn't get full webpage when piped

Thanks. I'll look into the methods offered.
I really appreciate the help!

Offline

Board footer

Powered by FluxBB