如何避免在bash中挂起wget
我有一个.sh脚本,其中包括来自谷歌的wgets货币汇率,如下所示:如何避免在bash中挂起wget,bash,wget,Bash,Wget,我有一个.sh脚本,其中包括来自谷歌的wgets货币汇率,如下所示: printf 'Bash: Going to get 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.co
printf 'Bash: Going to get 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
printf'Bash:获取汇率'
回音
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=aud“| sed'/res/!Ds/]*>///g'>exrates
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=jpy“| sed'/res/!Ds/]*>//g'>>exrates
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=hkd“| sed'/res/!Ds/]*>//g'>>exrates
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=nzd“| sed'/res/!Ds/]*>//g'>>exrates
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=eur“| sed'/res/!Ds/]*>//g'>>exrates
wget-qO-http://www.google.com/finance/converter?a=1&from=usd&to=gbp“| sed'/res/!Ds/]*>//g'>>exrates
mv/home/stan/perl/2014/scripts/exrates/home/stan/perl/2014/exrates/exrates
printf'Bash:获得汇率'
回声
不过,脚本偶尔挂在这里。我不介意每次运行时都更新这些速率,如果它挂起,我想完全跳过这一步,但是如何
我应该在“if”语句中输入什么来检查wget是否能够及时获取数据,或者是否需要花费很长时间?在wget执行过程中多说一点也不会有什么坏处
顺便说一句,我不知道为什么wget挂起。浏览器可以打开这些页面,同样的命令也可以从终端逐行运行。我假设它挂起,因为脚本中有大量HTTP请求被发送到单个主机。有问题的主机不太喜欢这样,它开始阻止来自您的IP地址的请求 一个简单的解决方法是在请求之间放置一个
睡眠
。您还可以使用以下功能:
getExchangeRates() {
wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=$1" | sed '/res/!d;s/<[^>]*>//g' >> exrates
sleep 10 # Adding a 10 second sleep
}
getExchangeRates aud
该函数也可以在不同货币的循环中调用:
for currency in aud jpy hkd nzd eur gpb; do
getExchangeRates $currency
done
仅在wget语句中使用超时
wget --timeout 10 <URL>
wget——超时10
超时时间以秒为单位
放一些
在两个wget之间睡眠wget有各种超时选项。从手册页
--timeout=seconds
Set the network timeout to seconds seconds. This is equivalent to
specifying --dns-timeout, --connect-timeout, and --read-timeout,
all at the same time.
因此,您可以简单地设置--timeout,或者如果您认为这只是其他因素之一,您可以设置特定的超时以某种方式调用此函数不起作用,不会传递或识别任何参数。@Stangosagain传递给函数的参数中有一个输入错误。现在已修复。@devnull知道了,要么是“for currency..do getExchangeRates$currency”要么是“for i..do getExchangeRates$i”。现在工作。大的thanks@Stangoesagain很好,它起作用了。您可能需要参考。