如何在Bash脚本中正确转义引号?

时间:2014-06-09 06:11:32

标签: bash shell escaping quotes

我有一个卷曲,必须在终端中执行 -

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

所以我试过这个。我想我在逃避报价时犯了一个错误,我已经尝试了#34; ..."并手动\#39;对于url中的每个单引号并用" ..."

包装它们

我还应该尝试什么?

1 个答案:

答案 0 :(得分:3)

您可能需要将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
相关问题