Harshit Laddha
Harshit Laddha

Reputation: 2124

How to properly escape quotes in Bash scripts?

I have a curl which has to be executed like this in terminal -

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

Now i want to create a bash script that can execute this script -

#!/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

so i tried this. I think i am making a mistake while escaping quotes, i already tried "..." and manually \' for every single quote in url and wrapping them with "..."

What else should i try to?

Upvotes: 2

Views: 373

Answers (1)

konsolebox
konsolebox

Reputation: 75608

Perhaps what you need is storing those arguments for curl in an array. Arrays save their elements from being modified with word splitting and pathname expansion when you already expand them.

#!/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

And simplify your loop further:

: > 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

Upvotes: 3

Related Questions