3

When I use actual values on curl command in the next script, it's working and giving me a result back, but when I use variables it does not work properly

I think this is issue with how I define them on the command

amz_t=$(cat amazon-token.txt )
flx_id=$(cat flex-id.txt )
ses_t=$(cat session-token.txt )

curl -s -H 'Host: flex-capacity-na.amazon.com' \
  -H 'Cookie: session-token='$ses_t'' \
  -H 'x-amz-access-token: '$amz_t'' \
  -H 'x-flex-instance-id: '$flx_id'' \
  -H 'Accept: */*' \
  -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' \
  -H 'Accept-Language: en-us' \
  --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

This is the command I try to run in script above mention txt files only contain the certain values no garbage values.

5

Try something like this:

amz_t=$(cat amazon-token.txt)
flx_id=$(cat flex-id.txt)
ses_t=$(cat session-token.txt)

UA='iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141'

URL='https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122'

curl -s -H 'Host: flex-capacity-na.amazon.com' \
  -H "Cookie: session-token=$ses_t" \
  -H "x-amz-access-token: $amz_t" \
  -H "x-flex-instance-id: $flx_id" \
  -H 'Accept: */*' \
  -H "User-Agent: $UA" \
  -H 'Accept-Language: en-us' \
  --compressed "$URL" >> output.txt

Use single-quotes for fixed strings (i.e. without any variables in them) and double-quotes for strings that need variable interpolation to take place.

2

You can't use single quotes on your variables. This will cause bash to not interpret the $ special character. You can use double quotes instead. http://tldp.org/LDP/abs/html/quoting.html

EDIT

I realize now you are closing all your single quotes before and re-opening them after your variables but the variables are still probably being read incorrectly because they aren't quoted. I'm not sure you even need to single quote everything in this command but if you do you can still put your double quotes in like below:

Change your line:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H 'Cookie: session-token='$ses_t'' -H 'x-amz-access-token: '$amz_t'' -H 'x-flex-instance-id: '$flx_id'' -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

To:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H 'Cookie: session-token='"$ses_t" -H 'x-amz-access-token: '"$amz_t" -H 'x-flex-instance-id: '"$flx_id" -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt

But I think this would work too:

curl -s -H 'Host: flex-capacity-na.amazon.com' -H "Cookie: session-token=$ses_t" -H "x-amz-access-token: $amz_t" -H "x-flex-instance-id: $flx_id" -H 'Accept: */*' -H 'User-Agent: iOS/10.2.2 (iPhone Darwin) Model/iPhone Platform/iPhone6,1 RabbitiOS/2.0.141' -H 'Accept-Language: en-us' --compressed 'https://flex-capacity-na.amazon.com/GetOffersForProvider?serviceAreaIds=122' >> output.txt
  • I'd suggest making this more readable by adding extra (escaped) linefeeds. Also by using variables for long header contents like the URL and the User-Agent header. There's no easy way to read the code in your answer, so there's no easy way to tell whether it's correct or not. – cas Aug 15 '17 at 9:29
  • 1
    Also, (from what I can see without scrollling back and forth too much) your first suggested alternative should work but is unnecessarily complicated, I'd just get rid of it. The second one, with entire header names and variabled enclosed in double-quotes as one string is much better. – cas Aug 15 '17 at 9:31
  • thank you mate it work as expected thank you for the filling my knowledge gaps – Dan Rox Aug 15 '17 at 13:37
  • No problem, Cas provided a much easier to read and better breakdown of the issue in his answer. – Jesse_b Aug 15 '17 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.