我需要像这样执行curl命令:
#!/bin/bash
shopid=9932781
itemid=231873991
curl -sSb /tmp/cookies 'https://website.com' -H 'cookie: csrftoken=mytoken' -H 'x-csrftoken: mytoken' -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary' -H 'referer: https://website.com' \
--data-binary $'------WebKitFormBoundary\r\nContent-Disposition: form-data; name="shopid"\r\n\r\n${shopid}\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="itemid"\r\n\r\n${itemid}\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="quantity"\r\n\r\n1\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="donot_add_quantity"\r\n\r\nfalse\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="update_checkout_only"\r\n\r\nfalse\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="source"\r\n\r\n\r\n------WebKitFormBoundary\r\nContent-Disposition: form-data; name="checkout"\r\n\r\ntrue\r\n------WebKitFormBoundary--\r\n'
$''
引号是必要的,否则(即在双引号的情况下)\r\n
将不起作用 - 但使用此表单$shopid
和{{1}不会被他们的价值取代。
我怎样才能同时获得这两种行为?
答案 0 :(得分:3)
您需要使该代码可维护
binary_data=$( cat <<END_DATA | sed 's/$/\r/'
------WebKitFormBoundary
Content-Disposition: form-data; name="shopid"
${shopid}
------WebKitFormBoundary
Content-Disposition: form-data; name="itemid"
${itemid}
------WebKitFormBoundary
Content-Disposition: form-data; name="quantity"
1
------WebKitFormBoundary
Content-Disposition: form-data; name="donot_add_quantity"
false
------WebKitFormBoundary
Content-Disposition: form-data; name="update_checkout_only"
false
------WebKitFormBoundary
Content-Disposition: form-data; name="source"
------WebKitFormBoundary
Content-Disposition: form-data; name="checkout"
true
------WebKitFormBoundary--
END_DATA
)
curl_opts=(
-sSb /tmp/cookies
-H 'cookie: csrftoken=mytoken'
-H 'x-csrftoken: mytoken'
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary'
-H 'referer: https://website.com'
--data-binary "$binary_data"
)
curl "${curl_opts[@]}" 'https://website.com'
答案 1 :(得分:1)
变量不扩展为单引号&#39;。
了解引用的工作原理非常重要:
&#34;双引号&#34;每个包含空格/元字符和每个扩展的文字:
"$var"
,"$(command "$var")"
,"${array[@]}"
,"a & b"
。使用'single quotes'
代码或文字$'s: 'Costs $5 US'
,ssh host 'echo "$HOSTNAME"'
。看到 http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words
所以,试试这个:
data="------WebKitFormBoundary
Content-Disposition: form-data; name='shopid'
${shopid}
------WebKitFormBoundary
Content-Disposition: form-data; name='itemid'
${itemid}
------WebKitFormBoundary
Content-Disposition: form-data; name='quantity'
1
------WebKitFormBoundary
Content-Disposition: form-data; name='donot_add_quantity'
false
------WebKitFormBoundary
Content-Disposition: form-data; name='update_checkout_only'
false
------WebKitFormBoundary
Content-Disposition: form-data; name='source'
------WebKitFormBoundary
Content-Disposition: form-data; name='checkout'
true
------WebKitFormBoundary--"
data="$(sed 's/$/\r/' <<< "$data")"
curl -sSb /tmp/cookies \
-H 'cookie: csrftoken=mytoken' \
-H 'x-csrftoken: mytoken' \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary' \
-H 'referer: https://website.com' \
--data-binary "$data" \
'https://website.com'
答案 2 :(得分:1)
您可以在单个字符串中使用多个引用样式。因此:
$'Constant\r\n\n\r\n'"$bar"
...已使用\r\n
规则解析$'...'
,但$bar
扩展了双引号规则(以便进行扩展)。