如何避免在bash中挂wget
发布时间:2020-12-15 22:09:47 所属栏目:安全 来源:网络整理
导读:我有.sh脚本,其中包括从谷歌获得货币汇率,如下所示: printf 'Bash: Going to get exchange rates'echo wget -qO- "http://www.google.com/finance/converter?a=1from=usdto=aud" | sed '/res/!d;s/[^]*//g' exrateswget -qO- "http://www.google.com/financ
我有.sh脚本,其中包括从谷歌获得货币汇率,如下所示:
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 但是,偶尔会有脚本挂起.我不介意每次运行都不更新这些费率,如果它挂起我想完全跳过这一步,但是怎么样? 我应该在“if”语句中添加什么来检查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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |