How to avoid hanging wget in bash -
i have .sh script that, among other things, wgets currency exchange rates google follows:
printf 'bash: going exchange rates' echo wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=aud" | sed '/res/!d;s/<[^>]*>//g' > exrates wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=jpy" | sed '/res/!d;s/<[^>]*>//g' >> exrates wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=hkd" | sed '/res/!d;s/<[^>]*>//g' >> exrates wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=nzd" | sed '/res/!d;s/<[^>]*>//g' >> exrates wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=eur" | sed '/res/!d;s/<[^>]*>//g' >> exrates wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=gbp" | sed '/res/!d;s/<[^>]*>//g' >> exrates mv /home/stan/perl/2014/scripts/exrates /home/stan/perl/2014/exrates/exrates printf 'bash: got exchange rates' echo
occasionally script hangs here, however. don't mind not updating these rates every time runs, if hangs i'd skip step altogether, how?
what should put in "if" statement check if wget can fetch data promptly or take forever? little more verbosity in wget execution wouldn't hurt either.
btw, don't know why wget hangs. browser opens pages okay, , same commands run terminal line line work, too.
i assume hangs because have number of http requests being sent single host in script. host in question doesn't , starts block requests ip address.
a simple workaround put sleep
in between requests. make use of function:
getexchangerates() { wget -qo- "http://www.google.com/finance/converter?a=1&from=usd&to=$1" | sed '/res/!d;s/<[^>]*>//g' >> exrates sleep 10 # adding 10 second sleep }
and invoke passing parameter function:
getexchangerates aud
the function invoked in loop various currencies:
for currency in aud jpy hkd nzd eur gpb; getexchangerates $currency done
Comments
Post a Comment