在 ShellScript 中根据 Curl 输出分配变量
我正在尝试编写一个 shell 脚本,它将为curl 输出分配一个变量。当我从命令行运行这个curl命令时,它的输出是这样的:
$curl "test.test.com/landing/505978290?c=Area%20Rug"
www.test.com/test/test?test=test
转到这个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
但是,当我运行 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
如果我尝试打印 url,我会得到一个空字符串。无论字符串是否正确匹配,我都会得到这个。为什么curl在shell脚本中的操作与在命令行中的操作不同。
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:
$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 = $(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:
% 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分配 url 时不应有空格,试试这个 :-
注意:curl -s 是抑制进度表
There should no have spaces when assign url, try this :-
note : curl -s is to suppress the progress meter