bash 在 ShellScript 中根据 Curl 输出分配变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8418505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:09:07  来源:igfitidea点击:

In ShellScript Assign Variable Based on Curl Output

linuxbashshellcurl

提问by Spencer

I am trying to write a shellscript that will assign a variable to curl output. The output of this curl command when I run it from the command line is this:

我正在尝试编写一个 shellscript,它将为 curl 输出分配一个变量。当我从命令行运行 curl 命令时,它的输出是这样的:

$curl "test.test.com/landing/505978290?c=Area%20Rug"
www.test.com/test/test?test=test

Going to this url is supposed to simply output a text document with another url. I want to test that this second url is the url it is supposed to be.

转到这个 url 应该只是输出一个带有另一个 url 的文本文档。我想测试这个第二个 url 是它应该是的 url。

   url = $(curl "test.test.com/landing/505978290?c=Area%20Rug")
   if [$url -gt "www.tazinga.com/landing/505978290?c=area%20rug&si=gw&alt_css=silver&sv=34&"]         echo ""test.test.com/landing/505978290?c=Area%20Rug did not redirect properly"
    else
        echo "Right URL"
    fi

However, when I run the shellscript I get this output:

但是,当我运行 shellscript 时,我得到以下输出:

       % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    75    0    75    0     0    997      0 --:--:-- --:--:-- --:--:--  1013
Command not found. 
-bash: [: missing `]'
yes

If I try to print out url, I get an empty string. I get this regardless as to whether or not the string correctly matches or not. Why is curl operating differently in a shellscript than from the command line.

如果我尝试打印出 url,我会得到一个空字符串。无论字符串是否正确匹配,我都会得到这个。为什么 curl 在 shellscript 中的运行方式与在命令行中的运行方式不同。

回答by ajreal

There should no have spaces when assign url, try this :-

分配 url 时不应该有空格,试试这个:-

url=$(curl -s ...)

note : curl -s is to suppress the progress meter

注意: curl -s 是抑制进度表