【问题标题】:How to echo variables inside an ANSI quoted string如何在 ANSI 引用的字符串中回显变量
【发布时间】:2018-03-21 21:37:44
【问题描述】:

我需要像这样执行 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$item 不会被替换为它们的值.

如何获得这两种行为?

【问题讨论】:

标签: bash curl quotes


【解决方案1】:

您需要使该代码可维护

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' 

【讨论】:

  • 也许有点 DRYer 会将边界放入变量中。
  • DRYer 是什么意思?
  • DRY=不要重复自己
  • 意味着你不剪切和粘贴代码,你使用函数来重用它;你不重复文字,你定义常量变量或类似的。
【解决方案2】:

您可以在单个字符串中使用多种引用样式。因此:

$'Constant\r\n\n\r\n'"$bar"

...\r\ns 使用 $'...' 规则解析,但 $bar 使用双引号规则扩展(这样扩展也会发生)。

【讨论】:

    【解决方案3】:

    变量以“单引号”扩展。

    了解引用的工作原理很重要:

    “双引号”每个包含空格/元字符和每个扩展的文字:"$var""$(command "$var")""${array[@]}""a &amp; 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' 
    

    【讨论】:

    • 我只需要知道如何使 curl 命令工作,我知道单引号不会返回变量,但简单地将单引号替换为该 curl 命令上的双引号不会解决问题。
    • 因为使用双引号也解决不了问题
    • 谢谢,但我会将发布数据放在一个变量中,以使 curl 命令更短一些。
    • 注意数据中的引用方式,防止冲突或需要反斜杠/保护双引号
    • 我刚刚尝试了您的解决方案,但没有成功,看来您必须像上面 glenn jackman 的回答一样使用 sed
    猜你喜欢
    • 2020-02-17
    • 1970-01-01
    • 2018-10-01
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多