In ShellScript Assign Variable Based on Curl Outpu

2019-05-30 17:29发布

问题:

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.

回答1:

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

url=$(curl -s ...)

note : curl -s is to suppress the progress meter