简体   繁体   中英

Bash Shell Curl JSON variable pass

I was trying to do CURL call from shell script and command line argument.

url=$1
pg=$2
coockie=$3

curl '$url' -H 'sec-fetch-mode: cors' -H 'origin: $url' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36' -H 'content-type: application/json;charset=UTF-8' -H 'accept: application/json, text/plain, */*' -H 'referer: $url' -H 'authority: recruiting.adp.com' -H '$cookie' -H 'sec-fetch-site: same-origin' --data-binary $'{"filters":[{"name":"country","label":"Country"},{"name":"state","label":"State"},{"name":"grp","label":"Job Type"},{"name":"ptitle","label":"Job"}],"results":{"pageTitle":"Search Results","zeroResultsMessage":"We\'re sorry but we have no job openings at this time that match your search criteria. Please try another search.","searchFailureMessage":"Oops\u0021 Something went wrong.  Search has encountered a problem. Try searching again","resultsFoundLabel":"results found","bookmarkText":"Bookmark This","pageSize":"100","sortOrder":"00001000","shareText":"Share","fields":[{"name":"ptitle","label":"Published Job Title"},{"name":"location","label":"Location"},{"name":"position","label":"Position"}]},"pagefilter":{"page": 3},"rl":"enUS"}' --compressed

echo "$url $pg"

But "pagefilter":{"page": 3},"rl":"enUS"}' in this page attribute script is working when it's a numeric value but i want to have it like "pagefilter":{"page": "$page"},"rl":"enUS"}'

Unable to understand why it's not working. Tried escaping double quotation too.

Input is like /s.sh "URL" 3 1234

Wrap your variable with double/single/double quotes "'" will help in most cases

page=3
curl -d'{"page":"'"$page"'"}' http://...

This will be expanded to

curl -d'{"page":"3"}' http://...

Or use only single/double qoutes '" to expand to a number:

curl -d'{"page":'"$page"'}' http://...

Will be expandend to

curl '-d{"page":3}'

Thanks to @jschnnasse Tried with bash -x and found out that only '$pg' taking the value "pg" , "'$pg'" and '"$pg"' not working following code is working perfectly.

url=$1
pg=$2
coockie=$3

curl '$url' -H 'sec-fetch-mode: cors' -H 'origin: $url' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36' -H 'content-type: application/json;charset=UTF-8' -H 'accept: application/json, text/plain, */*' -H 'referer: $url' -H 'authority: recruiting.adp.com' -H '$cookie' -H 'sec-fetch-site: same-origin' --data-binary $'{"filters":[{"name":"country","label":"Country"},{"name":"state","label":"State"},{"name":"grp","label":"Job Type"},{"name":"ptitle","label":"Job"}],"results":{"pageTitle":"Search Results","zeroResultsMessage":"We\'re sorry but we have no job openings at this time that match your search criteria. Please try another search.","searchFailureMessage":"Oops\u0021 Something went wrong.  Search has encountered a problem. Try searching again","resultsFoundLabel":"results found","bookmarkText":"Bookmark This","pageSize":"100","sortOrder":"00001000","shareText":"Share","fields":[{"name":"ptitle","label":"Published Job Title"},{"name":"location","label":"Location"},{"name":"position","label":"Position"}]},"pagefilter":{"page": '$pg'},"rl":"enUS"}' --compressed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM