简体   繁体   English

如何根据wget的状态进行分支?

[英]How can I branch based on the status of wget?

I'm busy compiling a script to automatically install MySQL on Linux by sending a PuTTy window a number of commands. 我正忙于通过向PuTTy窗口发送许多命令来编译脚本以在Linux上自动安装MySQL。

The issue I'm having is that the download link of the one repository keeps on changing, I need to have a prompt pop up if the link is not accessible. 我遇到的问题是一个存储库的下载链接不断变化,如果无法访问该链接,我需要弹出一个提示。

Here's what I got so far: 这是到目前为止我得到的:

wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 
&& echo "WE GOT IT" || echo "URL Invalid, Please specify new URL:" 
&& read newurl && wget $newurl

But on running the result regardless of if the file exists is: 1. The first file will download, it will display either echo message and then it will wait on the read newurl command for user input and then wget that new variable. 但是无论文件是否存在,运行结果时:1.第一个文件将下载,将显示回显消息,然后将等待read newurl命令以供用户输入,然后获取该新变量。

How do I make the whole second half after the "||" 我如何在“ ||”之后制作整个下半场 only occur if the first url doesn't work? 仅在第一个网址不起作用时才会发生?

Refactor Your Code 重构您的代码

Your code is too contorted. 您的代码太扭曲了。 While you could improve it with braced expressions, you're much better off refactoring your logic into if/else statements. 尽管可以使用大括号表达式来改进它,但是最好将逻辑重构为if / else语句。 As an (untested) example: 作为(未试用的)示例:

if wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
then
    echo 'We got it!'
else
    read -p 'URL invalid. Please specify new URL: '
    wget "$REPLY"
fi
wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 
&& echo "WE GOT IT" && exit || echo "URL Invalid, Please specify new URL:" 
&& read newurl && wget $newurl


Add "exit" on left side of ||  with && operator ,,if the left argument is true then it will download that file and exit from terminal otherwise prompt for new url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM