Koshur
Koshur

Reputation: 478

curl working on command line but not inside shell script

When I'm using the value of variable cookie directly in the curl command on command line - it works; but it doesn't work inside the script. Following error:

#!/bin/bash

cookie=`tail -1000 cat.txt | grep -v "wm-ueug" | grep -v "JRECookie" | grep "JSESSIONID.*_ga=GA" | tail -1 | sed -r 's/.{26}//' | sed 's/.$//'`

`curl "http://example.com/monitor?method=monitor&refresh=true&count=4&start=1&dateFrom=2017-02-21&dateTo=2017-02-28&runId=&hidden_status=&dojo.preventCache=1488290723103" -H "Host: example.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20400202 Firefox/50.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Content-Type: application/x-www-form-urlencoded" -H "X-Requested-With: XMLHttpRequest" -H "Referer: http://example.com/monitor" -H "Cookie: $cookie" -H "Connection: keep-alive"`

Update: Removed back ticks - Don't see any error now but no output.

Upvotes: 2

Views: 2509

Answers (1)

chepner
chepner

Reputation: 532112

The main change to make is to run curl outside of a command subsitution:

# Not `curl ...`
curl ...

However, you may want to break up the curl command to make it more readable and understandable.

#!/bin/bash

cookie=$(tail -1000 cat.txt |
         grep -v "wm-ueug" |
         grep -v "JRECookie" |
         grep "JSESSIONID.*_ga=GA" |
         tail -1 | sed -r 's/.{26}//' | sed 's/.$//')

# Parameters can be passed to curl via the -d option, rather
# than as a query string in the URL.
url="http://example.com/monitor"
parameters=(
   -d method=monitor
   -d refresh=true
   -d count=4
   -d start=1
   -d dateFrom=2017-02-21
   -d dateTo=2017-02-28
   -d runId=hidden_status
   -d dojo.preventCache=1488290723103
)

headers=(
   -H "Host: example.com"
   -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20400202 Firefox/50.0"
   -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
   -H "Accept-Language: en-US,en;q=0.5"
   -H "Content-Type: application/x-www-form-urlencoded"
   -H "X-Requested-With: XMLHttpRequest"
   -H "Referer: http://example.com/monitor"
   -H "Cookie: $cookie"
   -H "Connection: keep-alive"
)
curl --compressed "${parameters[@]}" "${headers[@]}" "$url"

Upvotes: 1

Related Questions