我怎样才能根据wget的状态进行分支?

时间:2013-01-06 19:58:10

标签: linux bash operators wget rpm

我正忙于编写一个脚本,通过向PuTTy窗口发送一些命令来自动在Linux上安装MySQL。

我遇到的问题是一个存储库的下载链接不断变化,如果链接无法访问,我需要弹出一个提示。

这是我到目前为止所得到的:

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

但无论文件是否存在,运行结果都是:1。第一个文件将下载,它将显示echo消息,然后它将等待读取newurl命令以进行用户输入,然后输出新变量。

如何在“||”之后制作整个后半部分只有在第一个网址不起作用时才会出现?

2 个答案:

答案 0 :(得分:1)

重构您的代码

您的代码太扭曲了。虽然您可以使用支撑表达式来改进它,但您最好将逻辑重构为if / else语句。作为(未经测试的)示例:

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

答案 1 :(得分:0)

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