【问题标题】:How to properly escape quotes in Bash scripts?如何正确转义 Bash 脚本中的引号?
【发布时间】:2014-06-09 06:11:32
【问题描述】:

我有一个 curl 必须在终端中像这样执行 -

curl 'http://realm124.c2.godfather.wonderhill.com/api/cities/102658334/marches.json' -H 'Origin: https://realm124.c2.godfather.wonderhill.com' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: */*' -H 'Cookie: fbm_287074358031811=base_domain=.wonderhill.com; __utma=94537411.474736185.1401299353.1402275697.1402289977.107; __utmb=94537411.6.10.1402289977; __utmc=94537411; __utmz=94537411.1402289977.107.107.utmcsr=c1.godfather.wonderhill.com|utmccn=(referral)|utmcmd=referral|utmcct=/platforms/kabam; _gc_session3=4aba823576caf80d3c995886bb3df9be' -H 'Connection: keep-alive' -H 'DNT: 1' --data '%5Fsession%5Fid=3c634e07be6dab5934cf55231ea2d54b&march%5By%5D=649&march%5Bfloor%5D=1&march%5Bx%5D=101&gangster=6b718eb417e483c58dda639878966c050c31d6cc&march%5Bunits%5D=%7B%22MisterFixit%22%3A1000%2C%22Loanshark%22%3A5000%2C%22Butcher%22%3A264%2C%22Smuggler%22%3A1648%2C%22HatchetMan%22%3A1000%2C%22MisterKippy%22%3A1000%2C%22GMan%22%3A1100%2C%22TriggerMan%22%3A1797%2C%22MisterPao%22%3A1000%2C%22Highbinder%22%3A2000%2C%22Undertaker%22%3A1620%2C%22BlackWidow%22%3A691%2C%22Assassin%22%3A1280%2C%22Bartender%22%3A500%7D&user%5Fid=4927141&city%5Fid=102658334&%5Fmethod=post' | prettyjson

现在我想创建一个可以执行这个脚本的 bash 脚本 -

#!/bin/bash
url='http://realm124.c2.godfather.wonderhill.com/api/cities/102658334/marches.json' -H 'Origin: https://realm124.c2.godfather.wonderhill.com' -H 'Accept-Encoding: gzip,deflate,sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Accept: */*' -H 'Cookie: fbm_287074358031811=base_domain=.wonderhill.com; __utma=94537411.474736185.1401299353.1402275697.1402289977.107; __utmb=94537411.6.10.1402289977; __utmc=94537411; __utmz=94537411.1402289977.107.107.utmcsr=c1.godfather.wonderhill.com|utmccn=(referral)|utmcmd=referral|utmcct=/platforms/kabam; _gc_session3=4aba823576caf80d3c995886bb3df9be' -H 'Connection: keep-alive' -H 'DNT: 1' --data '%5Fsession%5Fid=3c634e07be6dab5934cf55231ea2d54b&march%5By%5D=649&march%5Bfloor%5D=1&march%5Bx%5D=101&gangster=6b718eb417e483c58dda639878966c050c31d6cc&march%5Bunits%5D=%7B%22MisterFixit%22%3A1000%2C%22Loanshark%22%3A5000%2C%22Butcher%22%3A264%2C%22Smuggler%22%3A1648%2C%22HatchetMan%22%3A1000%2C%22MisterKippy%22%3A1000%2C%22GMan%22%3A1100%2C%22TriggerMan%22%3A1797%2C%22MisterPao%22%3A1000%2C%22Highbinder%22%3A2000%2C%22Undertaker%22%3A1620%2C%22BlackWidow%22%3A691%2C%22Assassin%22%3A1280%2C%22Bartender%22%3A500%7D&user%5Fid=4927141&city%5Fid=102658334&%5Fmethod=post' --compressed
x=1
while [ $x -le 40 ]
do
    content=$(curl $url)
    echo $url $i
    echo $content >> output.txt
    sleep 120
    x=$(( $x + 1 ))
done

所以我尝试了这个。我想我在转义引号时犯了一个错误,我已经尝试过 "..." 并手动为 url 中的每个单引号 \' 并用 "..." 包装它们

我还应该尝试什么?

【问题讨论】:

  • 你得到了什么确切的错误?
  • 使用多个变量来存储curl参数如urloriginacc_lang...
  • 我得到了 - 第 2 行:-H:未找到未转义字符串 url 的命令,如果我包裹在“...”中,我得到 - 协议 'http 在 libcurl 中不受支持或禁用,如果我我得到了手动转义 - curl:(6)无法解析主机:realm124.c2.godfather.wonderhill.com\' curl:(6)无法解析主机:gzip,deflate,sdch \' curl:(6)无法解析主机:en-US,en;q=0.8\' curl:(6)无法解析主机:Mozilla curl:(6)无法解析主机:(X11; ...这样的错误

标签: bash shell escaping quotes


【解决方案1】:

也许您需要将curl 的这些参数存储在一个数组中。当您已经展开它们时,数组会保存它们的元素,以免被分词和路径名扩展所修改。

#!/bin/bash
curl_args=('http://realm124.c2.godfather.wonderhill.com/api/cities/102658334/marches.json'
           -H 'Origin: https://realm124.c2.godfather.wonderhill.com'
           -H 'Accept-Encoding: gzip,deflate,sdch'
           -H 'Accept-Language: en-US,en;q=0.8'
           -H 'User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36'
           -H 'Content-Type: application/x-www-form-urlencoded'
           -H 'Accept: */*'
           -H 'Cookie: fbm_287074358031811=base_domain=.wonderhill.com; __utma=94537411.474736185.1401299353.1402275697.1402289977.107; __utmb=94537411.6.10.1402289977; __utmc=94537411; __utmz=94537411.1402289977.107.107.utmcsr=c1.godfather.wonderhill.com|utmccn=(referral)|utmcmd=referral|utmcct=/platforms/kabam; _gc_session3=4aba823576caf80d3c995886bb3df9be'
           -H 'Connection: keep-alive'
           -H 'DNT: 1' --data '%5Fsession%5Fid=3c634e07be6dab5934cf55231ea2d54b&march%5By%5D=649&march%5Bfloor%5D=1&march%5Bx%5D=101&gangster=6b718eb417e483c58dda639878966c050c31d6cc&march%5Bunits%5D=%7B%22MisterFixit%22%3A1000%2C%22Loanshark%22%3A5000%2C%22Butcher%22%3A264%2C%22Smuggler%22%3A1648%2C%22HatchetMan%22%3A1000%2C%22MisterKippy%22%3A1000%2C%22GMan%22%3A1100%2C%22TriggerMan%22%3A1797%2C%22MisterPao%22%3A1000%2C%22Highbinder%22%3A2000%2C%22Undertaker%22%3A1620%2C%22BlackWidow%22%3A691%2C%22Assassin%22%3A1280%2C%22Bartender%22%3A500%7D&user%5Fid=4927141&city%5Fid=102658334&%5Fmethod=post'
           --compressed)
x=1
while [[ x -le 40 ]]
do
    content=$(curl "${curl_args[@]}")  ## Gets it done well.
    echo "$url $i"
    echo "$content" >> output.txt
    sleep 120
    x=$(( $x + 1 ))
done

并进一步简化您的循环:

: > output.txt ## Perhaps you need to truncate file first?

for ((x = 1; x <= 40; ++x)); do
    content=$(exec curl "${curl_args[@]}")  ## Optionally skip unnecessary forking with `exec`.
    echo "$url $i"
    echo "$content" >> output.txt
    sleep 120
done

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2017-06-04
    • 2012-07-26
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多