Linux 当wget返回错误404-Bash时如何恢复循环
我试图写一个简单的脚本,将下载一个文件为我,但我不知道确切的网址。在URL的中间有一个ID,在我得到正确的ID之前,我想增加它。问题是起点不正确,WGET返回404错误,然后循环不继续。p> 如何使循环使用新id重试 这是我的密码:Linux 当wget返回错误404-Bash时如何恢复循环,linux,bash,wget,Linux,Bash,Wget,我试图写一个简单的脚本,将下载一个文件为我,但我不知道确切的网址。在URL的中间有一个ID,在我得到正确的ID之前,我想增加它。问题是起点不正确,WGET返回404错误,然后循环不继续。p> 如何使循环使用新id重试 这是我的密码: #!/bin/sh id=7887902 urla="https://firsthalfofurl" urlb="secondhalfofurl.pdf" url=$urla$id$urlb for i in {1..5}; do wget --use
#!/bin/sh
id=7887902
urla="https://firsthalfofurl"
urlb="secondhalfofurl.pdf"
url=$urla$id$urlb
for i in {1..5};
do
wget --user uname --password pass $url;
id=$((id+1));
url=$urla$id$urlb
done
我还尝试在wget命令中的$url之后添加| | true,但没有成功 试试这个:
wget --user uname --password pass $url || true;
这样,如果wget失败,该行的结果仍然为零,脚本将继续。您应该将所有
$url
双引号。因为我们看不到first-
和secondhalfour
是什么,所以其中可能有一些关键字符破坏了您的命令
例如
好的,我找到了解决办法。它并不完美,因为结果是一个只能通过ctrl-c终止的无限循环。这是我的新代码
#!/bin/sh
id=8057270
urla="firsthalfofurl"
urlb="secondhalfofurl.pdf"
checka="$(ls -l | wc -l)"
checkb="$(ls -l | wc -l)"
url=$urla$id$urlb
update () {
id=$((id+1));
url=$urla$id$urlb;
checkb="$(ls -l | wc -l)";
}
yes () {
#check to see if the number of files in the folder has increased.
if [ $checkb -gt $checka ]; then
#this will show up on every successive loop so if I let it run overnight I will know if the file has been found. Not really necessary.
sleep 3;
echo File Found;
sleep 1;
fi;
update;
wget --user username --password pass $url;
trap yes EXIT
yes
}
yes
嗯……只有设置了
e
选项,这不是很有帮助吗?我想这不是默认的行为。我在发布之前就试过了。这是我得到的错误:2015-04-18 17:04:42错误404:未找到如何运行脚本?脚本看起来应该重复wget
5次,即使每次wget
都失败。我假设它也是这样运行的,但第一次迭代中的404错误似乎导致脚本退出循环,而不是使用新值重试。是否设置了set-e set?这似乎是有可能的,因为“正常”循环将继续,即使wget失败。您可能想查看更多关于它的讨论。当我创建变量时,它们是双引号。我在wget调用中在$url周围添加了双引号,但它仍然不起作用,关于url=$urla$id$urlb
?如果我从一个正确的id值开始,我就知道代码起作用,因为我知道一个文件的id,如果该id被设置为$id的初始值,它就会起作用。
#!/bin/sh
id=8057270
urla="firsthalfofurl"
urlb="secondhalfofurl.pdf"
checka="$(ls -l | wc -l)"
checkb="$(ls -l | wc -l)"
url=$urla$id$urlb
update () {
id=$((id+1));
url=$urla$id$urlb;
checkb="$(ls -l | wc -l)";
}
yes () {
#check to see if the number of files in the folder has increased.
if [ $checkb -gt $checka ]; then
#this will show up on every successive loop so if I let it run overnight I will know if the file has been found. Not really necessary.
sleep 3;
echo File Found;
sleep 1;
fi;
update;
wget --user username --password pass $url;
trap yes EXIT
yes
}
yes